简体   繁体   English

奇怪的Javascript JSON未定义错误

[英]Bizarre Javascript JSON undefined error

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. 我正在使用ASP.NET开发支持AJAX的WCF Web服务。 In my ASP.NET master page's <asp:ScriptManager>, I included the json.js file, copied fresh from json.org. 在我的ASP.NET母版页的<asp:ScriptManager>中,我包含了json.js文件,该文件是从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.js中的第一行代码(遵循大量注释)失败(VS 2008捕获Javascript异常),这是:

JSON = JSON || {};

The error says that JSON is undefined: 该错误表明JSON未定义:

Microsoft JScript runtime error: 'JSON' is undefined Microsoft JScript运行时错误:'JSON'未定义

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. 最后我听说在Javascript中对未定义的变量执行此类操作不是错误。

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. 我没有使用Javascript或ASP.NET的丰富经验,所以可能是我在设置中缺少一些常见问题。

I would make make sure that your page is rendering in IE8 standards mode; 我会确保您的页面在IE8标准模式下呈现; I found that if the document is rendering in Quirks or IE7 mode, this.JSON is undefined. 我发现如果文档是在Quirks或IE7模式下渲染,则this.JSON是未定义的。

You can force IE8 to render in IE8 standards mode by including the following meta tag on your page: 您可以通过在页面上包含以下元标记来强制IE8在IE8标准模式下呈现:

<meta http-equiv="x-ua-compatible" content="IE=8" />

You should be using json2.js . 你应该使用json2.js The offending line has been changed: 违规行已更改:

// 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 = {};
}

As mentioned, you should be using json2. 如前所述,您应该使用json2。

The error, however, stems from MS handling of global variables. 然而,错误源于MS处理全局变量。 Try window.JSON = window.JSON || 试试window.JSON = window.JSON || {}; {}; From then on, JSON should work just fine. 从那时起,JSON应该可以正常工作。

you might have to do var JSON = JSON || {}; 你可能要做var JSON = JSON || {}; var JSON = JSON || {}; I have run in to similar problems with Javascript in IE8. 我在IE8中遇到过类似的Javascript问题。

I had the same problem trying to use google maps "streetview". 我尝试使用谷歌地图“街景”时遇到了同样的问题。 The streetview window would turn completely black and I'd get the icon in the status bar indicating an error. 街景窗口会变成全黑,我会在状态栏中看到一个错误指示。 I disabled DivX as webdev007 did and the problem was resolved! 我在webdev007上禁用了D​​ivX,问题解决了! Webdev, you're a genius! Webdev,你是个天才! Thanks! 谢谢!

I have the same error and I was already using json2; 我有同样的错误,我已经在使用json2;

For me it works when I add var before the expression: 对我来说,当我在表达式之前添加var时它可以工作:

var JSON = JSON || {};

I recently started receiving the JSON undefined JavaScript error and found the problem was due to a recent DivX add on in IE. 我最近开始收到JSON未定义的JavaScript错误,发现问题是由于最近在IE中添加了DivX。 The DivX add on also caused the secure content warnings on web pages that had all https references and no http ones (I set my Display mixed content to Prompt for correcting references during web development). DivX添加也导致在具有所有https引用且没有http引用的网页上发出安全内容警告(我将显示混合内容设置为在Web开发期间更正引用的提示)。 After I disabled the DivX add on, the JSON undefined JavaScript error disappeared as well as the erroneous secure content warning pop ups. 禁用DivX添加后,JSON未定义的JavaScript错误消失,以及错误的安全内容警告弹出窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM