JSONP is different from JSON. JSON is a data serialization format, when you make a request waiting for a JSON the server must return a text content. And in fact, that's what your server is returning.
[{bla},{bla},{bla}]
JSONP (JSON with padding ), on the other hand, expects the response to be a JavaScript script. Because? Precisely because the <script>
tag is one of the exceptions to the Same Source Policy (and therefore a domain may include scripts from another domain). For this to work, however, a "trick" is needed:
The browser creates a script
element, placing the URL to be queried and an additional parameter specifying the name of a function . Example:
<script type="text/javascript" src="http://dominio/url?callback=myCallback">
( myCallback
is just an example - any other function name could be used)
The server prepares the JSON response, normal, but at the time of sending, it does it with content-type text/javascript
(or application/javascript
, but if I remember correctly this gives problem in some older browsers) and most importantly: putting a padding :
myCallback([{bla}, {bla}, {bla}]);
Without this myCallback(
at the beginning and );
at the end the JSONP call does not work - after all, everything that is going to come is a normal JavaScript object:
[{bla}, {bla}, {bla}]
It will not run any code, nor will it be saved in any variable, and nothing will happen ...
You apparently modified the server to serve the correct content-type , but forgot to add padding . This is why jQuery - noting that the script was loaded and executed correctly, but the callback function was not called - acknowledges the error.
The solution is to modify the server to do the padding (remember: it is to use the name set in the parameter query string , not necessarily myCallback
). Now, if the server is not yours, but third-party (so you can not modify it), I'm afraid your only output is to use a proxy (for example, causing your own server access the third-party server and return the result to the client). In that case, you will not need to mess with JSONP (nor CORS) - you can just use JSON and you're done ...