JSONP is different from JSON. JSON is a data serialization format, when you make a request waiting for a JSON the server must return 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 answer to be a Javascript script. Why? Precisely because the tag <script>
is one of the exceptions to the Same Origin Policy (and therefore a domain can include scripts on another domain page). For this to work, however, it takes a "trick":
The browser creates an element script
, placing the URL to be consulted and an additional parameter specifying the name of a function. Example:
<script type="text/javascript" src="http://dominio/url?callback=myCallback">
(myCallback
is just one example - any other function name could be used)
The server prepares the JSON response, normal, only when sending, it does it with the content-type text/javascript
(or application/javascript
, but if I remember correctly this gives problem in some older browsers) and the most important: 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, all that will come is a normal Javascript object:
[{bla}, {bla}, {bla}]
that will not execute any code, nor will it be saved in any variable, and nothing will happen...
Apparently you modified the server to serve the content-type correct, but forgot to add the padding. That’s why jQuery - found that the script was loaded and executed correctly, but the function of callback was not called - accuses error.
The solution is to modify the server to do the padding (remembering: is to use the name established in the parameter of query string, not necessarily myCallback
). Now, if the server is not yours, but from a third party (so you can’t modify it), I’m afraid your only output is to use a proxy (for example, causing your own server to access the third party server and return the result to the client). In this case, you don’t need to mess with JSONP (or CORS) - you can just use JSON and you’re done...
This is because you are using an external domain without CORS (I think).
– Guilherme Nascimento
This will only work if the server responds with
Content-type: application/javascript
.– André Ribeiro
The answer seems common JSON, already tried with
dataType: 'json'
?– bfavaretto
Your jsfiddle example: http://jsfiddle.net/o3tbgbvm/ Gives the following error message: Resource Interpreted as scripts but transferred with MIME type text/Plain.
– MarceloBoni
bfavaretto, thank you for your attention. Using datatype as 'json' would not work because it is a cross-Domain request, q requires jsonp.
– Milhomens
Qmechanic73, thanks in advance for your attention, even modifying the script according to your suggestion the return pointed by the console was: "myCallback was not called". I had already tried this approach ( and even then the 'bigown' denied me by claiming: "lack of research"...)
– Milhomens
How do you know who has denied you and why? I, who am a moderator, don’t know. I don’t see anything to indicate who did it, or why.
– bfavaretto
@Milmen I did not deny your question. Least of all you, because the website does not allow negative people. I edited to improve the style of the question. I was helping you without changing the essence of the question. Worse, I don’t know why you decided to say that I claimed something. Where did you get it?
– Maniero
Ladies and gentlemen, I’m sorry for the inconvenience of the misunderstanding. It was just a misinterpretation here on my part. Sincere apologies and thanks to the effort of each to help in their own way.
– Milhomens