GhostInTheSecureShell
GhostInTheSecureShell

Reputation: 1010

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.

Upvotes: 4

Views: 132

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039298

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.

Upvotes: 8

Related Questions