javascript 为什么 JSON.stringify 给出 JSON 未定义错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17173153/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:21:54  来源:igfitidea点击:

Why JSON.stringify giving JSON undefined error?

javascriptjsonextjs

提问by JS11

I am trying to use JSON.stringifyin my javascript page (EXTJS) code but it's giving me JSON is undefined error. Error happends only in IE (ver.10). This code will work in FF and Chrome. Below is a sample code that I am trying to use. Why is it failing? This seems like a valid example and works in other browsers. Thanks in advance.

我正在尝试JSON.stringify在我的 javascript 页面 (EXTJS) 代码中使用,但它给了我 JSON is undefined 错误。错误仅在 IE (ver.10) 中发生。此代码适用于 FF 和 Chrome。下面是我尝试使用的示例代码。为什么会失败?这似乎是一个有效的示例,并且适用于其他浏览器。提前致谢。

var contact = new Object(); 
contact.firstname = "Joe";
contact.surname = "Smith";
contact.gendere= "male";
var jsonText = JSON.stringify(contact);

采纳答案by SheetJS

Use <!DOCTYPE html>if you only need to support IE10. If you need to use earlier versions of IE (some people still need to support IE6) you can use the JS implementation of JSON: https://github.com/douglascrockford/JSON-js

使用<!DOCTYPE html>,如果你只需要支持IE10。如果需要使用较早版本的IE(有些人还是需要支持IE6)可以使用JSON的JS实现:https: //github.com/douglascrockford/JSON-js

回答by Denys Séguret

Put

<!DOCTYPE html>

at the top of your document (and of course remove any other doctype you might have).

在文档的顶部(当然,删除您可能拥有的任何其他文档类型)。

Without it, IE10 is in a weird compatibility mode where many "new" features aren't available, for example JSON.

没有它,IE10 处于一种奇怪的兼容模式,其中许多“新”功能不可用,例如JSON.

回答by dbrin

You have the framework already, use it: Ext.JSON.encode/decode http://docs.sencha.com/extjs/4.1.3/#!/api/Ext.JSON

你已经有了框架,使用它:Ext.JSON.encode/decode http://docs.sencha.com/extjs/4.1.3/#!/api/Ext.JSON