4

I'm experiencing an error that I haven't been able to find any mention of anywhere. I'm developing an AJAX-enabled WCF web service with ASP.NET. In my ASP.NET master page's <asp:ScriptManager>, I included the json.js file, copied fresh from json.org. When I run the page, it fails (VS 2008 catches a Javascript exception) on the first line of code in json.js (following lots of comments), which is:

JSON = JSON || {};

The error says that JSON is undefined:

Microsoft JScript runtime error: 'JSON' is undefined

Well, duh! That's why the line is testing if it's defined and if so setting it to an empty object! It is supposed to be undefined, right? Last I heard it was not an error in Javascript to perform such an operation on an undefined variable.

Can anyone give me a clue as to what's going on here? I suspect it's something gone wrong elsewhere that's somehow causing this problem. I don't have deep experience with either Javascript or ASP.NET so it might be that I'm missing some common gotcha in the setup.

4

7 回答 7

8

我会确保您的页面以 IE8 标准模式呈现;我发现如果文档以 Quirks 或 IE7 模式呈现,this.JSON则未定义。

您可以通过在页面上包含以下元标记来强制 IE8 以 IE8 标准模式呈现:

<meta http-equiv="x-ua-compatible" content="IE=8" />
于 2011-04-27T22:30:07.237 回答
7

您应该使用json2.js。违规行已更改:

// Create a JSON object only if one does not already exist. We create the
// methods in a closure to avoid creating global variables.

if (!this.JSON) {
    this.JSON = {};
}
于 2009-11-26T06:20:13.660 回答
1

如前所述,您应该使用 json2。

但是,该错误源于 MS 对全局变量的处理。试试 window.JSON = window.JSON || {}; 从那时起,JSON 应该可以正常工作了。

于 2009-11-26T06:23:06.133 回答
1

你可能不得不这样做var JSON = JSON || {}; 我在 IE8 中遇到了类似的 Javascript 问题。

于 2009-11-26T06:24:07.770 回答
1

我在尝试使用谷歌地图“街景”时遇到了同样的问题。街景窗口会完全变黑,我会在状态栏中看到指示错误的图标。我像 webdev007 一样禁用了 DivX,问题就解决了!Webdev,你是个天才!谢谢!

于 2011-01-22T08:05:02.903 回答
0

我有同样的错误,我已经在使用 json2;

对我来说,当我var在表达式之前添加它时它起作用:

var JSON = JSON || {};
于 2010-08-17T18:31:45.557 回答
0

我最近开始收到 JSON 未定义的 JavaScript 错误,发现问题是由于最近在 IE 中添加了 DivX。DivX 附加组件还在包含所有 https 引用而没有 http 引用的网页上引起安全内容警告(我将显示混合内容设置为提示以在 Web 开发期间更正引用)。在我禁用 DivX 插件后,JSON 未定义的 JavaScript 错误消失了,并且弹出了错误的安全内容警告。

于 2011-01-16T13:52:27.873 回答