9

I'm using IE 10 Compatibility on Windows 8, I'm getting this error while running the WebApp in Debug mode(VS 2012). 0x800a1391 - JavaScript runtime error: 'JSON' is undefined Code below

var data = JSON.stringify(GetUserDetails());

function GetUserDetails() {
    var userData = {};
    userData.userName = $('#txtUserName').val();
    userData.password = $('#txtPassword').val();
    return userData;
}

Also , the strange fact is the same code hosted in IIS 8 runs fine in IE 10 Browser. Any inputs?

Rameez Ahmed Sayad
  • 1,300
  • 6
  • 16
  • 29
  • This is not answer to your question but i just want to add that when you are dealing with IE browser you shouldn't depend it to Convert object to JSON or JSON to object. I suggest use some samll other JS libraries to do such functions like [json2.js](https://github.com/douglascrockford/JSON-js) . – ebram khalil Mar 12 '13 at 11:51
  • 2
    I had the tag in my markup but continued to receive the error until I de-selected Compatibility View in the IE 10 tools menu. – MDA Jun 26 '13 at 13:56
  • Yes, I believe that is the expected behavior because JSON will work only in the standard mode as per the answers. – Rameez Ahmed Sayad Jun 27 '13 at 09:02

4 Answers4

8

IE8 and up only have the JSON object in standards mode. So you need to make sure the document has a doctype.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    Thanks , this looks to be the answer ... When I check IE 10 has it and IE 10 compatibility doesn't have it. IE 10 has like whereas IE 10 Compatibility has as ... not sure why it was commented .... – Rameez Ahmed Sayad Mar 12 '13 at 18:04
  • @RameezAhmedSayad: IE10 definitely has `JSON`. Again, it's only available in standards mode. – T.J. Crowder Mar 12 '13 at 18:05
3

I found out for me that it was an IE 10 compatibility's intranet setting that was raising this exception for my intranet sites:

"Display the intranet sites in Compatibility View"

To change this flag, or just to simply disable it, display the "Command Bar" in IE 10, then select "Tools" > "Compatibility View Settings" and un-check the option - second from the bottom. Fixed the issue immediately.

enter image description here

Bern
  • 7,808
  • 5
  • 37
  • 47
1

You need to include json2.js

see json2 inclusion

for more details. There are cases where we have to deal with cases where clients use IE7 standards. So our code had to cater to such cases too. Ours being a retail website is accessed by hundreds of clients who use IE7 standards. Including json2 solves this issue

Mohammed Rafeeq
  • 2,586
  • 25
  • 26
0

I had the same "JavaScript runtime error ...JSON is undefined" running Windows 8 and IE 10, while trying to run an ASP .NET application using Visual Studio 2010 development web server. When I opened the browser, clicked "Alt" to display the menu, then clicked Tools->Compatibility View Settings and unchecked "Display all websites in compatibility view", I stopped getting the error.