Search code examples
javascriptjqueryajaxgoogle-apiautosuggest

How to work with Google Suggest queries using jQuery


I am trying to work with the Google API for suggest queries. But I am getting an error.

Here is my code:

$.ajax({
  url:'http://suggestqueries.google.com/complete/search?client=chrome&q=sheikh+hasina',
  type:"GET",
  dataType: 'jsonp',
  jsonp:function (data) {
    console.log('yes');
  },
  async:'true',
  success:function (data) {
    console.log('yes');
  },
  error: function(jqXHR, textStatus, errorThrown){
    console.log(jqXHR);
    console.log(textStatus);
    console.log(errorThrown);
  }
});

Solution

  • Assuming that you're trying to get JSONP data from a URL, try this instead.

    $.ajax({
      url: 'http://suggestqueries.google.com/complete/search?client=chrome&q=sheikh+hasina',
      type: 'GET',
      dataType: 'jsonp',
      success: function (data) {
        console.log(data);
      },
      error: function(jqXHR, textStatus, errorThrown){
        console.log(jqXHR);
        console.log(textStatus);
        console.log(errorThrown);
      }
    });