0

I'm making a request to Google Custom Search using getJSON. FireBug is showing that it's returning a 200 OK. However, my callback function never happens.

The jQuery docs mention a silent error, which I suppose makes sense, yes? I'm not surprised Google would with a format error.

http://api.jquery.com/jQuery.getJSON/

How can trouble shoot this? Is there a workaround? I saw something about using AJAX instead but I can't seem to nail down the necessary details.

btw, if I copy the GET request URL out of Firebug and paste it into the browser I get results. But in Firebug results is blanks/empty. I'm stumped.

Chief Alchemist
  • 634
  • 2
  • 7
  • 19
  • get more details. 200 OK doesn't mean something didn't go wrong. Delve deeper, see the exact text of what's being returned from Google's server. I often get php errors/warnings mixed in with a JSON result (on my server, not an external api), which can wreak havok on the javascript trying to parse it. – deltree Apr 19 '12 at 00:07
  • "Can you post the JSON you get from Google?" From Firebug? Well, the results tab is empty / blank. – Chief Alchemist Apr 19 '12 at 22:14
  • "get more details. 200 OK doesn't mean something didn't go wrong" I owuld be glad to but I'm a bit of a n00b and unless I get specifics I'm not sure what I can give you, where it is, etc. I'm not an idiot. Just a noob. Thanks – Chief Alchemist Apr 19 '12 at 22:15

1 Answers1

1

The first thing I would check is that your request is using the JSONP protocol. Your URL should have the "callback=?" parameter included. JSONP is required for all cross-domain JSON requests.

If you are still having issues I would take a look at the return data in the debugger and post that here for others to review.

SEE THIS POST FOR YOUR SOLUTION: Access-Control-Allow-Origin error sending a jQuery Post to Google API's

Community
  • 1
  • 1
Shawn Lehner
  • 1,293
  • 7
  • 14
  • Thanks. I've done some more homework since I posted but still can't close it out. Can you please clarify "check is that your request is using the JSONP protocol". Call me a noob but...how to I check that? I also tried making the request with $.ajax() and same thing...I get a 200 but no data. Another sentence or two on "take a look at the return data in the debugger" would help me as well. Thanks again for you help. – Chief Alchemist Apr 19 '12 at 16:54
  • Make sure that in your initial request your URL contains the callback parameter (e.g. http://www.google.com?s=bleh&callback=?). Can you paste the same URL into your browser and see what it gets as return without using $.ajax? – Shawn Lehner Apr 19 '12 at 21:08
  • Yes, I copy the 200'ed (but no results) URL out of Firebug, paste it into the browser and that does return results. Therefore I believe the request is properly formatted, etc. it's something with the results. Again, thanks. Is there a way to PM you? I can send you the URL. I don't want to publish it since it's a dev set up of a Google Custom Search and I only get 100 queries per day - until I pay. – Chief Alchemist Apr 19 '12 at 22:12
  • Do you have an example of this code online anywhere I could take a look at ... the symptoms you are describing seem strange to me. – Shawn Lehner Apr 20 '12 at 16:30
  • In the demo you provided you are getting the following error which means it is not allowing cross domain request from your domain: XMLHttpRequest cannot load https://www.googleapis.com/customsearch/v1?key=yourkey&cx=yourcx0&start=10&q=test. Origin http://yourdomain.tld is not allowed by Access-Control-Allow-Origin. – Shawn Lehner Apr 20 '12 at 21:13
  • Thanks again for helping to get this sorted out. – Chief Alchemist Apr 24 '12 at 16:44