ajax parse,Ajax JSON Parse Error

  • Post author:
  • Post category:其他


Jquery 1.4 updated their JSON parser, so that it no longer accepts invalid JSON that it did before. Your server is outputting invalid JSON that was previously tolerated, but no longer is. This change was mentioned (briefly) in the release notes and documentation, and there’s been lots of discussion about it if you google “jquery 1.4 JSON”

But to sum up: the keys in JSON, to be valid, must have quotes around them, the string can’t be surrounded by those parentheses, and nothing can use single quotes as delimiters, so your JSON would need to be:

{“count”: 5, “success”: true, “error”: “”, “cache”: false, “data”: [{“column”: [“Mike”,”Mike”,”Steve”,”Steve”,”Steve”]}]}

or, more readably:

{

“count”: 5,

“success”: true,

“error”: “”,

“cache”: false,

“data”: [

{

“column”: [

“Mike”,

“Mike”,

“Steve”,

“Steve”,

“Steve”

]

}

]

}

The specs are very nicely and clearly spelled out at http://www.json.org/ with pictures and diagrams. You can also check your JSON at JSONLint – the JSON above checks out there just fine, but your original JSON raises all kinds of errors.

So as to what you need to do: if you have the ability to change what your server sends out, do that – make it output valid JSON. Please. Only if you are unable to change the server’s behavior, you’ll have to use a workaround from one of the posts linked above, or use some other JSON library.

General principle: in all cases, doing it the Right Way™ is preferable – fixing the server’s output (even if that’s filing a bug on an external server/API) is the long-term solution, but may not be immediately practical. So hackish workarounds are sometimes necessary, but not optimal.