1

in the implementation of the Amazon and Wikipedia Autocomplete API I am currently failing. Depending on the search parameter, another autocomplete service should be used. Unfortunately, none of the services work when Wikipedia (? Search = 5) is added. At Amazon API (?search=4) I only get to see the letters entered several times. I think both are the automatically displayed URL parameter "?callback=jSONXXXXXXXXX" blame.

Can you help me? The Google autocomplete works, but only if no other services like Amazon or Wikipedia are included.

    <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>

and the HTML-Input...

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

Greetings from Germany and thank you for your help!

Tobias
  • 49
  • 1
  • 8

1 Answers1

3

You are missing , after dataType : 'jsonp' line at wikipedia ajax call. AND data:jQuery.extend({action: 'opensearch'},{search: request.term}), instead of data: jQuery.extend({ action: 'opensearch', search: request.term }, { }),

And remove jsonp : false,jsonpCallback: 'jsonCallback',

Please find working code below

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">
Anup Yadav
  • 2,825
  • 3
  • 21
  • 30
  • Many Thanks. Now the Google and Amazon APIs are working again, but unfortunately there are still no Wikipedia suggestions displayed. – Tobias Feb 15 '18 at 06:18
  • `data:jQuery.extend({action: 'opensearch'},{search: request.term}), ` this needs to be replaced – Anup Yadav Feb 15 '18 at 06:22
  • Thank you. Nevertheless, I get no suggestions on Wikipedia. I am at a loss! Thanks anyway for your great help! – Tobias Feb 15 '18 at 06:23
  • Hey one more change is remove `jsonp : false,jsonpCallback: 'jsonCallback',` I found this working, though I only checked wikipedia's working by removing if conditions. – Anup Yadav Feb 15 '18 at 06:31
  • I do not know how to continue. It works, but I only see the first letter. https://prnt.sc/if3kbi – Tobias Feb 15 '18 at 13:18
  • I don't know logic behind `data[1]` and `value[0]` but use `value` instead of `value[0]` in wiki autocomplete – Anup Yadav Feb 15 '18 at 13:43
  • 1
    I learned it that way, but: It works! Thank you for your help! – Tobias Feb 15 '18 at 13:57
  • How can I limit youtube search suggestions keywords ? When select a keyword auto submit search form. – Mark Fulghum Sep 02 '20 at 18:41
  • 1
    @MarkFulghum though this is off topic, still I can help you, use like this, `response(results.slice(0, 10));` And one more link, you may find helpful https://stackoverflow.com/questions/7617373/limit-results-in-jquery-ui-autocomplete – Anup Yadav Sep 03 '20 at 04:32