Can't get JSON data from jQuery AJAX API call

喜欢而已 提交于 2021-02-11 16:35:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!