问题
My API URL returns the following JSON:
[{"_id":{"$id":"529c759d361ae724088b4568"},"name":"1877","soundcloud_url":"","genres":["rock","electro"]}]
Here is my jQuery AJAX call:
$.ajax({
url: gigniteAPI,
dataType: "jsonp",
complete: function (data) {
var ParsedObject = JSON.stringify(data);
alert(ParsedObject);
}
});
In chrome I can see the script call and that the data that is sent back. However when I JSON.stringify the result all I get is:
{"readyState":4,"status":200,"statusText":"success"}
Why is it not outputting my API data?
Is it to do with the square brackets in my response?
UPDATE:
Perhaps someone can get this jsfiddle to output the 'name' key from the json response? http://jsfiddle.net/T85eB/
回答1:
The complete
function receives the XHR object as a response. I believe you should be using .done(function...) to get the data:
This is taken from here: http://api.jquery.com/jquery.ajax/
$.ajax({
url: gigniteAPI,
dataType: "jsonp")
})
.done(function (data) {
var ParsedObject = JSON.stringify(data);
alert(ParsedObject);
}
})
;
来源:https://stackoverflow.com/questions/24313127/cant-get-json-data-from-jquery-ajax-api-call