Chris Glasier
Chris Glasier

Reputation: 811

JSON.stringify does not work in IE

I have used the following method:

JSON.stringify(json,null,2);

This produces tabbed output in all browsers except IE: in IE, it displays a single line. Is null the problem and if so what are the alternatives?

Upvotes: 3

Views: 18169

Answers (6)

Kaushik Ghosh
Kaushik Ghosh

Reputation: 14920

There may be situations where it may not be possible to include the Doctype or meta tag or nothing might work as in my case so I had to figure out this way below as explained.

To post json objects back to the server, json.strinfy will have to be supported. To support the same on IE, please download json2.js from https://github.com/douglascrockford/JSON-js and refer in your view. The following code snipped worked for me, I hope it helpe someone else too.

//include jquery library from your preferred cdn or local drive.
<!-- include json2.js only when you need JSON.stringfy method -->
<script type="text/javascript" src="~/scripts/json2.js"></script>
<script type="text/javascript">
function button_click() {
 //object to post back to the server.
 var postData = { "Id": $('#hfUserId').val(), "Name": $('#Name').text(), 
 "address": new Array() };
 var address = new Array(); var addr;
 addr = { "HouseNo": "1", "Street": "ABC", "City": "Chicago", "State": "IL" };
 address[0] = addr;
 addr = { "HouseNo": "2", "Street": "DEF", "City": "Hoffman Est", "State": "IL" };
 address[1] = addr;
//make ajax call to server to save the data
 $.ajax({
    url: '@Url.Action("myAction", "MyController")',
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify(postData),
    async: true,
    success: function (data) { alert('User Saved'); },
    error: function (data) { alert('Could not save User'); }
    });
}
</script>

The model for the address list will be as below. Please note that the property names are the same as the addr object and it has get and set.

public class Address
{
    public string HouseNo { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
}

The controller action will be something like below

[HttpPost]
public ActionResult myAction(string Id, string Name, List<Address> address)
{
   JsonResult result = null;
   result = new JsonResult
                {
                    Data = new
                    {
                        error = false,
                        message = "User Saved !"
                    }
                };
    return result;
}

Upvotes: 2

user2908290
user2908290

Reputation:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

Upvotes: 1

Machisuji
Machisuji

Reputation: 738

One correct doctype for IE8 to support it is:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

Upvotes: 3

DJ.
DJ.

Reputation: 394

You Can use jquery.min.js or latest or 1.8.2 version and put this in the <head> element:

<meta http-equiv="X-UA-Compatible" content="IE=8" />

It works in IE8 and above.

Upvotes: 5

Krishnan
Krishnan

Reputation: 1504

Ok.. This might not be the solution to your problem exactly but might help others coming here:

Yes, as Lycha mentioned, this is supported in IE8 and above.

So, if you are still getting a "JSON undefined" error, it must be a "DOCTYPE" problem.

Hence, mention the proper DOCTYPE (and preferably keep your document in valid xhtml format (i.e. , , tags etc)..) and things should work fine.

Not mentioning (proper) DOCTYPE generally creates a LOT of issues with CSS in IE, and in this case JSON too.

--

(IE === 'bad')

Upvotes: 3

NT3RP
NT3RP

Reputation: 15370

Taking a look at the MSDN documentation, it looks like Internet Explorer expects either a function or an object for the second argument, otherwise it throws an exception.

You could try to use undefined instead of null. For example:

JSON.stringify(json,undefined,2)

Upvotes: 2

Related Questions