0

I am trying to make an ajax call to the googlesuggest page that generates a xml. I use a small hack that seems to work and is documented here The code is this:

$.ajax({
  url: 'https://suggestqueries.google.com/complete/search',
  data: {
    client: 'firefox',
    q: word,
  },
  dataType: 'jsonp'
})
.done(function(dataWeGotViaJsonp){
  var len = dataWeGotViaJsonp.length;
  for(var i=0;i<len;i++){
    alert(dataWeGotViaJsonp[i]);
  }
});

I don't get any errors in the console on my extension but it doesn't generate anything. How should i make it work properly or what's wrong with it?

The data returned from this: looks like this:

["ob",["obama","obamacare","obituaries","obey","oblivion","obama phone","oberlin college","obama gun control","obagi","obsidian"]]

Where "ob" is the searched term.

Update:

This is the code i updated replacing the $.ajax with $.getJSON after the instructions from the first link i provided in this post.

function process(word){
  $.getJSON("https://suggestqueries.google.com/complete/search?callback=?",
    { 
      "jsonp":"suggestCallBack", // jsonp callback function name
      "q":word, // query term
      "client":"firefox" // force youtube style response, i.e. jsonp
    }
  );
  suggestCallBack= function(dataWeGotViaJsonp){
    alert("asdas");
    var len = dataWeGotViaJsonp.length;
    for(var i=0;i<len;i++){
      alert(dataWeGotViaJsonp[i]);
    }
  };
};

Update 2:

I replaced the keyword 'firefox' under client: specifier with 'youtube' and it now returns something like this:

    window.google.ac.h
    (
    ["ob",[["obama",0,[]],
    ["obamacare",0,[]],
    ["obituaries",0,[]],
    ["obey",0,[]],
    ["oblivion",0,[]],
    ["obama phone",0,[]],
    ["oberlin college",0,[]],
    ["obama gun control",0,[]],
    ["obagi",0,[]],
    ["obsidian",0,[]]],
    {"k":1,"q":"I3uqQqdI9GsurIoEbRJwRQ_P7Co"}]   
)

Which i don't know how to actually parse. I am curious why it is not working under the standard google search. A very good tool for testing purposes can be found on this JSfiddle .

Ta01
  • 31,040
  • 13
  • 70
  • 99
Edeph
  • 732
  • 2
  • 12
  • 29
  • You can put the method .fail to see if you get an error. And the Chrome developer console shows an error? – aelbaz Apr 23 '13 at 09:26
  • No error on any console. I will try with .fail now. I forgot to mention that i granted permission on csp like this: `"content_security_policy": "script-src 'self' https://suggestqueries.google.com/; object-src 'self'"` – Edeph Apr 23 '13 at 10:29
  • In the manifest file has given permission to the url: 'https://suggestqueries.google.com/complete/search'? – aelbaz Apr 23 '13 at 11:48
  • permission given to `"https://suggestqueries.google.com/complete/*"` precisely. – Edeph Apr 23 '13 at 12:24
  • Have you tried `https://suggestqueries.google.com` (*without* the trailing slash) in your CSP? – apsillers Apr 23 '13 at 14:00
  • @apsillers Yes, without any modification to the previous state. Still not working and no errors on console. – Edeph Apr 23 '13 at 15:25

1 Answers1

0

I managed to make it work but it acts a little different and a little weirder than this link specifies. The thing is i switched from $.ajax to a $.getJSON call and i used what it's been indicated in the documentation, and actually got the google results (checked) even though i used the client: 'youtube' specifier. I think now google uses that for youtube and google search or they got mixed up somehow.

$.getJSON("https://suggestqueries.google.com/complete/search?callback=?",
                { 
                  "jsonp":"suggestCallBack", // jsonp callback function name
                  "q":query, // query term
                  "client":"youtube" // force youtube style response, i.e. jsonp
                }
            );
    suggestCallBack = function (data) {
                var suggestions = []; //creates void array
                $.each(data[1], function(key, val) {
                    suggestions.push(val[0]); //moves every suggestion from the word into the array
                })
                suggestions.length = 1; //restricts the array to only the first element
                suggestions = []; //resets the array to void for future calls
            };

This worked perfectly for me and it should work for anyone. I think it might work with the $.ajax too but only using client: 'youtube'.

Edeph
  • 732
  • 2
  • 12
  • 29