Reputation: 314
My Javascript alert's undefined when trying to read the following JSON string:
[
{
"number_messages":"3"
},
{
"message1":"abc"
},
{
"message2":"c"
},
{
"message3":"a"
}
]
I am using AJAX to get this string (it gets trough correctly.
resp=jQuery.parseJSON(response);
alert( resp.number_messages );
My ajax call is:
$.ajax({
type: 'POST',
url: 'backend2.php',
data: dataString,
success: submitFinished,
dataType: 'json'
});
This alerts "undefined", no errors show up in dragonfly (it's like firebug but for opera).
I am quite new to the whole JSON JQuery thing, so it's prob. something really easy, but I can't seem to figure it out. I've searched for answers and edited my code for over an hour, and still haven't got it to work.
Upvotes: 0
Views: 1245
Reputation: 95031
jQuery automatically parses json data into an object, you dont need jQuery.parseJSON(response)
$.ajax({
"url":"page.php",
"dataType":"json",
"success":function(data){
alert(data[0].number_messages);
}
});
Upvotes: 0