0

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/

Cœur
  • 37,241
  • 25
  • 195
  • 267
Robjocky
  • 47
  • 1
  • 7

1 Answers1

3

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);

     }
  })

;

Hayes
  • 848
  • 4
  • 10
  • 1
    Why `.done()`, why not use the `success` property of `$.ajax`? Also, I need to double check if `.done()` would even work there - is it going to pick up `data` at all. – VLAZ Jun 19 '14 at 18:22
  • 1
    Well, `done(function(data) { /*...*/}) does work, but I still think `success` is more appropriate. – VLAZ Jun 19 '14 at 18:31
  • These do not seem to work. Here is a jsfiddle to test them out http://jsfiddle.net/T85eB/ – Robjocky Jun 19 '14 at 19:35
  • @Vld I think the direction jQuery is moving is towards methods like `.done` rather than options like `success:`. – Barmar Jun 19 '14 at 19:42