'JSON' 仅在 IE 中是未定义的错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5093582/
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' is undefined error in IE only
提问by KevinDeus
I'm making an AJAX call to a WCF service and when I pass in my data i use JSON.stringify()
我正在对 WCF 服务进行 AJAX 调用,当我传入我的数据时,我使用 JSON.stringify()
The call returns and works fine in FF, & Chrome, but not IE8. I get an error: 'JSON' is undefined
调用返回并在 FF 和 Chrome 中正常工作,但不适用于 IE8。我收到一个错误:'JSON' 未定义
suggestions?
建议?
p.s. I also want this to work in IE7
ps 我也希望它在 IE7 中工作
回答by river
Use json2 for a consistent cross browser implementation.
使用 json2 实现一致的跨浏览器实现。
回答by Joe Albowicz
I had the issue with IE9. IE9 was rendering my page in "quirks" mode, the solution was simply to add <!DOCTYPE html>. This took me out of "quirks" mode which I'm sure fixed more than just this issue!
我遇到了 IE9 的问题。IE9 以“怪癖”模式呈现我的页面,解决方案只是添加<!DOCTYPE html>. 这让我摆脱了“怪癖”模式,我确信解决的不仅仅是这个问题!
回答by Sujay
Update
更新
Check the JSON3library. It works like a charm.
检查JSON3库。它就像一个魅力。
I hope this helps.
我希望这有帮助。
Hope this helps. I got this from a few online sources long back. don't have their links.
Sorry that i'm unable to cite references.
希望这可以帮助。我很久以前从一些在线资源中得到了这个。没有他们的链接。
抱歉,我无法引用参考文献。
var JSON = JSON || {};
// implement JSON.stringify serialization
JSON.stringify = JSON.stringify || function(obj) {
var t = typeof (obj);
if (t != "object" || obj === null) {
// simple data type
if (t == "string")
obj = '"' + obj + '"';
return String(obj);
} else {
// recurse array or object
var n, v, json = [], arr = (obj && obj.constructor == Array);
for (n in obj) {
v = obj[n];
t = typeof (v);
if (t == "string")
v = '"' + v + '"';
else if (t == "object" && v !== null)
v = JSON.stringify(v);
json.push((arr ? "" : '"' + n + '":') + String(v));
}
return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
}
};
// implement JSON.parse de-serialization
JSON.parse = JSON.parse || function() {
var r = "(?:-?\b(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\b)", k = '(?:[^\0-\x08\x0a-\x1f"\\]|\\(?:["/\\bfnrt]|u[0-9A-Fa-f]{4}))';
k = '(?:"' + k + '*")';
var s = new RegExp(
"(?:false|true|null|[\{\}\[\]]|" + r + "|" + k + ")", "g"), t = new RegExp(
"\\(?:([^u])|u(.{4}))", "g"), u = {
'"' : '"',
"/" : "/",
"\" : "\",
b : "\u0008",
f : "\u000c",
n : "\n",
r : "\r",
t : "\t"
};
function v(h, j, e) {
return j ? u[j] : String.fromCharCode(parseInt(e, 16));
}
var w = new String(""), x = Object.hasOwnProperty;
return function(h, j) {
h = h.match(s);
var e, c = h[0], l = false;
if ("{" === c)
e = {};
else if ("[" === c)
e = [];
else {
e = [];
l = true;
}
for ( var b, d = [ e ], m = 1 - l, y = h.length; m = 0;)
delete f[i[g]];
}
return j.call(n, o, f);
};
e = p({
"" : e
}, "");
}
return e;
};
}();
回答by Mghost.friend
回答by arafeandur
JQuery 2.x is no longer compatible with IE 6-8. JQuery 2.0 beta 2 release notes
JQuery 2.x 不再与 IE 6-8 兼容。JQuery 2.0 beta 2 发行说明
I know the main question is in regard to older versions of JQuery, but this was causing the error for me. I installed JQuery 1.x, which is API-compatible with JQuery 2.x, and added the following detection code:
我知道主要问题是关于旧版本的 JQuery,但这导致了我的错误。我安装了 JQuery 1.x,它与 JQuery 2.x 的 API 兼容,并添加了以下检测代码:
<!--[if lt IE 9]>
<script src="js/jquery-1.11.1.min.js"></script>
<![endif]-->
<!--[if gte IE 9]>
<script src="js/jquery.min.js"></script>
<![endif]-->
回答by Sanjay
In IE open the compatibility view settings and remove the localhost from the listbox for "Websites you have added to Compatibility View". It worked for me.
在 IE 中,打开兼容性视图设置并从“您已添加到兼容性视图的网站”列表框中删除 localhost。它对我有用。

