0

I have a jquery ajax function that works just fine in going out and retrieving data. What I'm experiencing though is that I cannot seem to pull individual json data elements from the returned data using, what I thought was, conventional methods.

$("a[name$='-test']").click(function() {
  var rpc_url = '/history/get_test/' + $(this).attr('id');
  $.ajax({
         url : rpc_url,
        type : 'post',
 contentType : "application/json; charset=utf-8",
    datatype : 'json',
     success : function(data) {
       console.log(data.id);
    }
  }).done(function(data) {
    console.log(data.title);
  });
});

}

Original resulting data was formatted like this;

[{"id":"1","title":"Test title"}]

Then I modified my remote code to just return the data outside of an array context as such;

{"id":"1","title":"Test title"}

As you can see in my code, I tried accessing the data both at the success event and at the done event. Fiddler shows well formatted json as the returned data. Though both continue to yield undefined in the console output for data.id & data.title.

So, am I missing something obvious here?

Thanks.

2
  • Yes, you need to JSONDecode it before accessing it this way. (i guess) Commented Apr 11, 2015 at 6:21
  • 1
    Delete your contentType: setting, which describes sent data, not the response. Commented Apr 11, 2015 at 6:31

3 Answers 3

3

Try dataType:'json' instead of datatype:'json' ^^

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

1 Comment

LMAO!!! I so missed that! Yeah, that was it, alright! Thank you so much for catching that! :)
0

Try parsing the data first like this:

JSON.parse(data);

Comments

0

datatype changed to dataType

if not evaluate to JSON Object, you need to convert to Object manually, so you should do something like this:

console.log(JSON.parse(data)["title"])

Comments

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.