开发者

IE JSON parse works if from localhost fails otherwise

I'm attempting to track down a javascript exception I'm getting when parsing some JSON in IE that has been returned by an AJAX request. Normally I'd assume a JSON formatting error somewh开发者_高级运维ere in my code but in this case the JSON parses fine in Chrome, FireFox & IE but in IE's case ONLY if the URL is localhost. If I change the URL to my-laptop instead, which points to exactly the same location, the JSON fails to parse in IE but is still ok in Chrome and FireFox.

The JSON is very simple. The line below is from VS.Net's debugger so the double quotes on the ends are cosmetic:

data = "{ "ID" : 15, "Name" : "Hobart" } "

The JSON is being parsed with:

data = JSON.parse(data);

I've checked everything I can think of to see if the response is different between the response from localhost and my-laptop but can't spot anything. Why would IE decide not to parse the JSON when the server hostname changes?

Update: If I open up the localhost version with IE's compatibility mode switched on then the JSON paring alsi fails. Turn off compatibility mode and it works.


Chin Boons answer seems wrong. Keys in JSON need quotes and a value of type Number is fine without quotes.

When you are in compatibility mode, I would suppose that JSON is simply undefined. See e.g. here: Browser-native JSON support (window.JSON)

Maybe https://github.com/douglascrockford/JSON-js might help you in that case.


The JSON you provided is not a valid JSON structure. Every element needs to be wrapped up with an enclosing " ".

data = "{ "ID" : "15", "Name" : "Hobart" }"

Note the 15 is now "15".

Some weaker parser and or browser combination may have better tolerance for slight deviation from specification.


The cause of the issue is down to IE's compatibility mode. The comments in this question led me to open up the IE Developer Tools window and the web app was running in IE7 compatibility mode. I don't know why it was doing that and the compatibility mode button is not showing up in the IE address bar. Still at least I know what the problem is now and it's a more tractable problem to resolve than debugging reams of javascript.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜