Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does JSONP formatting have to be perfect? See example

I'm working with JSONP trying to fetch some results from the WOT API. See my code below:

  // Prepare the URL
  var url='http://api.mywot.com/0.4/public_link_json?hosts=amazon.co.uk/&callback=cbfunc';

  // Lookup
  $.getJSON(url, function(data){
    alert('success');
  });

Which seems to be failing for some reason (as I don'get an alert in my browser). After some research it seems the JSONP that is being returned has a space at the end of the callback function (between the very last curly bracket and closing bracket):

cbfunc({ "amazon.co.uk": { "target": "amazon.co.uk", "0": [ 95, 88 ], "1": [ 95, 87 ], "2": [ 95, 87 ], "4": [ 95, 87 ] } } )

After using an online JSON formatter (http://jsonformatter.curiousconcept.com/) it looks like this single space is throwing the entire $.getJSON() function as it can't handle the space?

Is JSONP formatting really that specific? I thought Javascript ignored whitespace? Have I diagnosed this correctly? Is there anything I can do to process the JSONP and remove the space?

Thanks in advance and I'm using jQUery BTW.

like image 870
GhostInTheSecureShell Avatar asked Feb 21 '23 01:02

GhostInTheSecureShell


1 Answers

Have I diagnosed this correctly?

No, this has nothing to do with the whitespace. Javascript is whitespace agnostic :-)

You should use callback=? in your url instead of callback=cbfunc as explained in the JSONP section of the documentation.

Here's the live demo: http://jsfiddle.net/Ssfk2/

It's up to jQuery to replace the ? with the name of the anonymous success callback that you are using.

like image 64
Darin Dimitrov Avatar answered Mar 02 '23 22:03

Darin Dimitrov