Javascript JSON.stringify 和 JSON.parse 在 IE9 中不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7146268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON.stringify and JSON.parse not working in IE9?
提问by saurabh ranu
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?
我正在使用JSON.Stringify
和JSON.parse
无处不在,它在 Firefox 上运行良好。它不再适用于 IE9,也不适用于 IE8。我能做什么?
采纳答案by Baz1nga
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
为什么你要依赖具有对象的浏览器而不是只包含 Douglas Crockford 的脚本文件.. 你可以在这里找到缩小的文件:http: //www.json.org/js.html
Once imported you dont have to worry abt the method existing in a browser.
导入后,您不必担心浏览器中存在的方法。
回答by phihag
JSON.stringify
starts with a lower-case s
. Both stringify
and parse
are available in IE8+, but only in standards mode.
JSON.stringify
以小写开头s
。双方stringify
并parse
都在IE8 +可用,但只有在标准模式。
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.
<!DOCTYPE html>
如果您当前正在使用 quirks 模式,请在您的文档前加上。另外,请注意您调用的 JavaScript 方法的大小写 - 所有内置方法都以小写字符开头。
回答by uch
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:
作为替代方案,在您可能出于某种原因需要在严格模式下运行的情况下(我有另一个包含“使用严格”的库),您可以在这里查看:https: //github.com/douglascrockford/JSON-js. 我对此进行了修改以首先检查 JSON 是否未定义,如果是,则仅生成函数 JSON.parse:
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.
我的问题是应用程序代码在 IE9 中不起作用(我相信参与的库使用了严格模式)。那为我解决了问题。
回答by Martin Zeitler
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.
唯一的问题是,发送 UTF-8 标头将使 JSON 无效(IE 不/不喜欢那样)。正如问题所描述的那样,这可能仍然适用于 IE9...几年前曾经写过一个how to。向可以解析原生 JSON 的浏览器添加 JSON 支持可能不是最佳解决方案,因为它会产生无用的开销 - 只是因为未能以预期的格式交付 JSON。