【问题标题】:Auotcomplete with JQuery UI not working?使用 JQuery UI 的自动完成功能不起作用?
【发布时间】:2018-07-25 19:39:25
【问题描述】:

在亚马逊和维基百科自动完成 API 的实施中,我目前失败了。根据搜索参数,应使用另一个自动完成服务。不幸的是,添加 Wikipedia (? Search = 5) 后,这些服务都不起作用。在 Amazon API (?search=4) 我只能看到输入了几次的字母。我认为两者都是自动显示的URL参数“?callback = jSONXXXXXXXXX”的责备。

你能帮帮我吗?谷歌自动完成功能有效,但前提是不包括亚马逊或维基百科等其他服务。

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

  <link rel='stylesheet prefetch' href='https://rsms.me/inter/inter-ui.css'>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css'>

      <link rel="stylesheet" href="css/style.css">
        <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" type="text/css" />
        <script src="//code.jquery.com/jquery-2.1.4.js" type="text/javascript"></script>
        <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js" type="text/javascript"></script>

    <script>
    if (window.location.search.indexOf('search=2') > -1) {
      jQuery(function() {
        jQuery( "#hyv-search" ).autocomplete({
          source: function( request, response ) {
            //console.log(request.term);
            var sqValue = [];
            jQuery.ajax({
                type: "POST",
                url: "http://suggestqueries.google.com/complete/search?hl=de&ds=yt&client=youtube&hjson=t&cp=1",
                dataType: 'jsonp',
                data: jQuery.extend({
                    q: request.term
                }, {  }),
                success: function(data){
                    console.log(data[1]);
                    obj = data[1];
                    jQuery.each( obj, function( key, value ) {
                        sqValue.push(value[0]);
                    });
                    response( sqValue);
                }
            });
          }
        });
      });
    } else if (window.location.search.indexOf('search=3') > -1){
      jQuery(function() {
        jQuery( "#hyv-search" ).autocomplete({
          source: function( request, response ) {
            //console.log(request.term);
            var sqValue = [];
            jQuery.ajax({
                type: "POST",
                url: "http://suggestqueries.google.com/complete/search?hl=de&ds=sh&client=youtube&hjson=t&cp=1",
                dataType: 'jsonp',
                data: jQuery.extend({
                    q: request.term
                }, {  }),
                success: function(data){
                    console.log(data[1]);
                    obj = data[1];
                    jQuery.each( obj, function( key, value ) {
                        sqValue.push(value[0]);
                    });
                    response( sqValue);
                }
            });
          }
        });
      });
    } else if (window.location.search.indexOf('search=4') > -1){
      jQuery(function() {
        jQuery( "#hyv-search" ).autocomplete({
          source: function( request, response ) {
            //console.log(request.term);
            var sqValue = [];
            jQuery.ajax({
                type: "POST",
                url: "https://completion.amazon.co.uk/search/complete?method=completion&mkt=4&p=Gateway&l=de_DE&b2b=0&fresh=0&sv=desktop&client=amazon-search-ui&x=String&search-alias=aps&ks=84",
                dataType: 'jsonp',
                data: jQuery.extend({
                    q: request.term
                }, {  }),
                success: function(data){
                    console.log(data[1]);
                    obj = data[1];
                    jQuery.each( obj, function( key, value ) {
                        sqValue.push(value[0]);
                    });
                    response( sqValue);
                }
            });
          }
        });
      });
    } else if (window.location.search.indexOf('search=5') > -1){
      jQuery(function() {
        jQuery( "#hyv-search" ).autocomplete({
          source: function( request, response ) {
            //console.log(request.term);
            var sqValue = [];
            jQuery.ajax({
                type: "POST",
                url: "https://de.wikipedia.org/w/api.php",
                jsonp : false,
                jsonpCallback: 'jsonCallback',
                // contentType: 'application/json', -- you can't set content type for a <script> tag, this option does nothing for jsonp | KevinB
                cache: 'true',
                dataType : 'jsonp'
                data: jQuery.extend({
                    action: 'opensearch',
                    search: request.term
                }, {  }),
                success: function(data){
                    console.log(data[1]);
                    obj = data[1];
                    jQuery.each( obj, function( key, value ) {
                        sqValue.push(value[0]);
                    });
                    response( sqValue);
                }
            });
          }
        });
      });
    } else {
      jQuery(function() {
        jQuery( "#hyv-search" ).autocomplete({
          source: function( request, response ) {
            //console.log(request.term);
            var sqValue = [];
            jQuery.ajax({
                type: "POST",
                url: "http://suggestqueries.google.com/complete/search?hl=de&client=youtube&hjson=t&cp=1",
                dataType: 'jsonp',
                data: jQuery.extend({
                    q: request.term
                }, {  }),
                success: function(data){
                    console.log(data[1]);
                    obj = data[1];
                    jQuery.each( obj, function( key, value ) {
                        sqValue.push(value[0]);
                    });
                    response( sqValue);
                }
            });
          }
        });
      });
    }
    </script>

和 HTML 输入...

<input id="hyv-search" class="ui-autocomplete-input" type="text" name="suche">

来自德国的问候,感谢您的帮助!

【问题讨论】:

  • 您可以添加html以获得更多说明吗?如果可能的话,然后添加堆栈代码 sn-p
  • 完成。感谢您的帮助!
  • 检查浏览器 console.log.. 你遇到什么样的错误?
  • jquery-2.1.4.js 文件应该放在 bootstrap.min.js 之前。
  • 控制台中没有任何内容。我不明白为什么...

标签: javascript php jquery ajax


【解决方案1】:

wikipedia ajax 调用的dataType : 'jsonp' 行之后,您缺少,。 和data:jQuery.extend({action: 'opensearch'},{search: request.term}), 而不是 data: jQuery.extend({ action: 'opensearch', search: request.term }, { }),

并删除jsonp : false,jsonpCallback: 'jsonCallback',

请在下面找到工作代码

if (window.location.search.indexOf('search=2') > -1) {
      jQuery(function() {
        jQuery( "#hyv-search" ).autocomplete({
          source: function( request, response ) {
            //console.log(request.term);
            var sqValue = [];
            jQuery.ajax({
                type: "POST",
                url: "http://suggestqueries.google.com/complete/search?hl=de&ds=yt&client=youtube&hjson=t&cp=1",
                dataType: 'jsonp',
                data: jQuery.extend({
                    q: request.term
                }, {  }),
                success: function(data){
                    console.log(data[1]);
                    obj = data[1];
                    jQuery.each( obj, function( key, value ) {
                        sqValue.push(value[0]);
                    });
                    response( sqValue);
                }
            });
          }
        });
      });
    } else if (window.location.search.indexOf('search=3') > -1){
      jQuery(function() {
        jQuery( "#hyv-search" ).autocomplete({
          source: function( request, response ) {
            //console.log(request.term);
            var sqValue = [];
            jQuery.ajax({
                type: "POST",
                url: "http://suggestqueries.google.com/complete/search?hl=de&ds=sh&client=youtube&hjson=t&cp=1",
                dataType: 'jsonp',
                data: jQuery.extend({
                    q: request.term
                }, {  }),
                success: function(data){
                    console.log(data[1]);
                    obj = data[1];
                    jQuery.each( obj, function( key, value ) {
                        sqValue.push(value[0]);
                    });
                    response( sqValue);
                }
            });
          }
        });
      });
    } else if (window.location.search.indexOf('search=4') > -1){
      jQuery(function() {
        jQuery( "#hyv-search" ).autocomplete({
          source: function( request, response ) {
            //console.log(request.term);
            var sqValue = [];
            jQuery.ajax({
                type: "POST",
                url: "https://completion.amazon.co.uk/search/complete?method=completion&mkt=4&p=Gateway&l=de_DE&b2b=0&fresh=0&sv=desktop&client=amazon-search-ui&x=String&search-alias=aps&ks=84",
                dataType: 'jsonp',
                data: jQuery.extend({
                    q: request.term
                }, {  }),
                success: function(data){
                    console.log(data[1]);
                    obj = data[1];
                    jQuery.each( obj, function( key, value ) {
                        sqValue.push(value[0]);
                    });
                    response( sqValue);
                }
            });
          }
        });
      });
    } else if (window.location.search.indexOf('search=5') > -1){
      jQuery(function() {
        jQuery( "#hyv-search" ).autocomplete({
          source: function( request, response ) {
            //console.log(request.term);
            var sqValue = [];
            jQuery.ajax({
                type: "POST",
                url: "https://de.wikipedia.org/w/api.php",
                // contentType: 'application/json', -- you can't set content type for a <script> tag, this option does nothing for jsonp | KevinB
                cache: 'true',
                dataType : 'jsonp',
                data: jQuery.extend({
                    action: 'opensearch'},
                    {search: request.term
                }, {  }),
                success: function(data){
                    console.log(data[1]);
                    obj = data[1];
                    jQuery.each( obj, function( key, value ) {
                        sqValue.push(value[0]);
                    });
                    response( sqValue);
                }
            });
          }
        });
      });
    } else {
      jQuery(function() {
        jQuery( "#hyv-search" ).autocomplete({
          source: function( request, response ) {
            //console.log(request.term);
            var sqValue = [];
            jQuery.ajax({
                type: "POST",
                url: "http://suggestqueries.google.com/complete/search?hl=de&client=youtube&hjson=t&cp=1",
                dataType: 'jsonp',
                data: jQuery.extend({
                    q: request.term
                }, {  }),
                success: function(data){
                    console.log(data[1]);
                    obj = data[1];
                    jQuery.each( obj, function( key, value ) {
                        sqValue.push(value[0]);
                    });
                    response( sqValue);
                }
            });
          }
        });
      });
    }
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" type="text/css" />
<script src="//code.jquery.com/jquery-2.1.4.js" type="text/javascript"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js" type="text/javascript"></script>


<input id="hyv-search" class="ui-autocomplete-input" type="text" name="suche">

【讨论】:

  • 非常感谢。现在 Google 和 Amazon API 又开始工作了,但遗憾的是仍然没有显示 Wikipedia 建议。
  • data:jQuery.extend({action: 'opensearch'},{search: request.term}), 这个需要更换
  • 谢谢。尽管如此,我在维基百科上没有得到任何建议。我很茫然!无论如何感谢您的大力帮助!
  • 我是这样学的,但是:它有效!感谢您的帮助!
  • @MarkFulghum 虽然这不是主题,但我仍然可以帮助你,像这样使用,response(results.slice(0, 10)); 还有一个链接,你可能会发现有帮助 stackoverflow.com/questions/7617373/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-26
  • 2012-12-16
  • 1970-01-01
  • 2014-01-16
相关资源
最近更新 更多