IN my web service,I return one json object in the method:
{name:'xx'}
I use the ajax to send the request,then parse them using the ‘eval’
onComplete:function(req){
var data=eval(req.responseText);
//do something according data
}
However,I can not get the ‘data’.
When I retrun the following string:
[{name:'xx'}]
It worked,and I get the ‘data’ as an array.
Through google,I know that it is caused by the ‘{‘ in the return string.
So I wonder if there is no way to retrun a json object ?
Have a read of this blog post on JSON hijacking. The author explains the issue your running into
Rather than adding
()around your JSON response, you should be usingJSON.parseor$.parseJSONlike gion_13 has said. Eval isn’t the way to go.Also, like quentin said in the comments, you need to change the JSON you’re returning as it’s invalid. You need to quote your key as well as your value.