开发者

Ajax JSON Parse Error

I find this kinda strange. it works on older versions of iQuery (ie 1.2.x to 1.3). However, i am getting a parserror when using jQuery 1.4.x

Any help will be appreciated. Thank you.

This is what i get from XMLHttpRequest.responseText

({count: 5, success: true, 开发者_StackOverflow中文版error: '', cache: false, data: [{column: ['Mike','Mike','Steve','Steve','Steve']}]})


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜