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.
I would make make sure that your page is rendering in IE8 standards mode; I found that if the document is rendering in Quirks or IE7 mode, this.JSON
is undefined.
You can force IE8 to render in IE8 standards mode by including the following meta tag on your page:
<meta http-equiv="x-ua-compatible" content="IE=8" />
You should be using 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.
The error, however, stems from MS handling of global variables. Try window.JSON = window.JSON || {}; From then on, JSON should work just fine.
you might have to do var JSON = JSON || {};
I have run in to similar problems with Javascript in IE8.
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! Webdev, you're a genius! Thanks!
I recently started receiving the JSON undefined JavaScript error and found the problem was due to a recent DivX add on in IE. 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). After I disabled the DivX add on, the JSON undefined JavaScript error disappeared as well as the erroneous secure content warning pop ups.
I have the same error and I was already using json2;
For me it works when I add var
before the expression:
var JSON = JSON || {};
精彩评论