Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

like image 270
Nate C-K Avatar asked Dec 03 '22 06:12

Nate C-K


1 Answers

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" />
like image 96
rob Avatar answered Dec 22 '22 00:12

rob