I am using jQuery Version 1.5.1 to do the following ajax call:
$.ajax({
dataType: 'jsonp',
data: { api_key : apiKey },
url: "http://de.dawanda.com/api/v1/" + resource + ".json",
success: function(data) { console.log(data); },
error: function(jqXHR, textStatus, errorThrown) { console.log(errorThrown); console.log(textStatus); }
});
The server responds with a valid json object:
{
"response": {
"type":"category",
"entries":1,
"params":{
"format":"json",
"api_key":"c9f11509529b219766a3d301d9c988ae9f6f67fb",
"id":"406",
"callback":"jQuery15109935275333671539_1300495251986",
"_":"1300495252693"
},
"pages":1,
"result":{
"category":{
"product_count":0,
"id":406,
"restful_path":"/categories/406",
"parent_id":null,
"name":"Oberteile"
}
}
}
}
But the success callback is never called, instead the error callback produces this output:
jQuery15109935275333671539_1300495251986 was not called
parsererror
Why does this happen?
I am using no additional libraries to jQuery.
EDIT:
If I try to make the ajax call with "json" as dataType instead of "jsonp", the server responds with an empty string.
JSONP requires that the response be wrapped in some kind of callback function, because it works by injecting a script tag into the document as a mechanism to load data from another domain.
Essentially, what happens is a script tag gets dynamically inserted into the document like so:
<script src="http://the.other.server.com/foo?callback=someFn"></script>
callback
is dependent on the resource you're calling, it's common for the parameter to be callback
though.
someFn
is then used to process the returned data from the server, so the server should respond with:
someFn({theData: 'here'});
The someFn is passed as part of the request, so the server needs to read it and wrap the data appropriately.
This is all assuming you're grabbing the content from another domain. If so, you're limited by the same origin policy: http://en.wikipedia.org/wiki/Same_origin_policy