JSON.stringify and JSON.parse not working in IE9?

2019-01-18 10:17发布

I'm using JSON.Stringify and JSON.parse everywhere and it works fine with Firefox. It's working no more with IE9 nor does it work in IE8. What can I do?

4条回答
乱世女痞
2楼-- · 2019-01-18 10:49

JSON.stringify starts with a lower-case s. Both stringify and parse are available in IE8+, but only in standards mode.

Prepend your document with <!DOCTYPE html> if you're currently using quirks mode. Also, watch the capitalization of the JavaScript methods you call - all built-in ones start with a lower-case character.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-01-18 10:58

the mere issue is, that sending UTF-8 headers will invalidate the JSON (IE doesn't/didn't like that). as the issue is described, that might still apply for IE9... once wrote a how to, a few years ago. adding JSON support to a browser which can parse native JSON is probably not the optimal solution, since it produces useless overhead - only because failing to deliver the JSON in the expected format.

查看更多
Evening l夕情丶
4楼-- · 2019-01-18 11:02

For an alternative, in a scenario where you might need to run in strict mode for whatever reason (I have another library that includes "use strict"), you can look here: https://github.com/douglascrockford/JSON-js. I modified this to check first if JSON is undefined, and only generate the function JSON.parse if it is:

if (typeof JSON === "undefined") {
    var JSON = {
        parse: <insert value of json_parse from library here>
    };
}

My issue was application code not working in IE9 (strict mode being used by a participating library, I believe). That solved the problem for me.

查看更多
Animai°情兽
5楼-- · 2019-01-18 11:09

why do you want to depend on the browser having the object instead just include the script file by Douglas Crockford.. You can find the minifed file here: http://www.json.org/js.html

Once imported you dont have to worry abt the method existing in a browser.

查看更多
登录 后发表回答