jQuery won't parse my JSON from AJAX query
Posted
by littlecharva
on Stack Overflow
See other posts from Stack Overflow
or by littlecharva
Published on 2008-10-30T09:47:17Z
Indexed on
2010/03/25
22:53 UTC
Read the original article
Hit count: 440
Hi,
I'm having difficulty parsing some JSON data returned from my server using jQuery.ajax()
To perform the AJAX I'm using:
$.ajax({
url: myUrl,
cache: false,
dataType: "json",
success: function(data){
...
},
error: function(e, xhr){
...
}
});
And if I return an array of items then it works fine:
[ { title: "One", key: "1" }, { title: "Two", key: "2" } ]
The success function is called and receives the correct object.
However, when I'm trying to return a single object:
{ title: "One", key: "1" }
The error function is called and xhr contains 'parsererror'. I've tried wrapping the JSON in parenthesis on the server before sending it down the wire, but it makes no difference. Yet if I paste the content into a string in Javascript and then use the eval() function, it evaluates it perfectly.
Any ideas what I'm doing wrong?
Anthony
© Stack Overflow or respective owner