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/

8
  • 6
    You API is returning JSON, not JSONP. Commented Jun 19, 2014 at 17:48
  • @Barmar What dataType should I use? dataType: json is for domain-same origin requests. Commented Jun 19, 2014 at 17:53
  • If it's a different domain, you need to fix the server so it returns JSONP instead of JSON. Commented Jun 19, 2014 at 17:54
  • JSONP response looks like callbackname(JSON); Commented Jun 19, 2014 at 17:56
  • 1
    Surely you'd want to use JSON.parse() instead of JSON.stringify() on the incoming data? Commented Jun 19, 2014 at 17:56

1 Answer 1

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

     }
  })

;

Sign up to request clarification or add additional context in comments.

4 Comments

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.
Well, done(function(data) { /*...*/}) does work, but I still think success` is more appropriate.
These do not seem to work. Here is a jsfiddle to test them out jsfiddle.net/T85eB
@Vld I think the direction jQuery is moving is towards methods like .done rather than options like success:.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.