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.
No, this has nothing to do with the whitespace. Javascript is whitespace agnostic 🙂
You should use
callback=?in your url instead ofcallback=cbfuncas 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.