Should JSONP formatting be perfect? See Example

I am working with JSONP trying to get 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'); }); 

It seems that for some reason it fails (as I am not warning in my browser). After some research, it seems that the returned JSONP takes place at the end of the callback function (between the most recent curly bracket and the closing bracket):

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

After using the online JSON formatting (http://jsonformatter.curiousconcept.com/), does it seem like this single space throws the whole $ .getJSON () function because it cannot handle the space?

Is JSONP formatting really specific? I thought Javascript ignores spaces? Did I get the diagnosis right? Is there anything I can do to handle JSONP and remove the space?

Thanks in advance, and I'm using jquery BTW.

+4
source share
1 answer

Did I get the diagnosis right?

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

Should you use callback=? in your URL instead of callback=cbfunc , as described in the JSONP section of the documentation .

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

Prior to jQuery to replace ? addressed to the anonymous success callback you are using.

+8
source

All Articles