JSON.stringify support for IE7

Background:

Are you using AJAX calls using JSON in your web app?  Are you using JSON.stringify to convert JavaScript objects to JSON?  Everything seems to be working just fine until you try it in IE7.

Here’s the problem, IE7 runs an older script engine than IE8 and has no full support for JSON.

The Simple Solution…

The simple solution to using JSON.stringify and needing to support IE7 is to reference Douglas Crockford’s json2.js script file available on GitHub, also available on NuGet.

Sample Code

<script type="text/javascript" src="scripts/json2.js"></script>
<script type="text/javascript">
function myAjaxMethod(id) {
		var dataObject = new Object();
		dataObject.FirstName = $("#txtFirstName").val();
		dataObject.LastName = $("#txtLastName").val();

		var json = JSON.stringify(dataObject);

		//Ajax POST to execute identity verification...
		$.ajax({
			type: 'POST',
			url: YOUR_URL,
			data: json,
			contentType: 'application/json; charset=utf-8',
			dataType: 'json',
			cache: false,
			timeout: 30000,
			success: onSuccessHandler,
			error: onErrorHandler
		});
}
</script>

Additional Comments

The official json2.js file is not minified. If you want to reference a minified version, you must manually minify the script file. There are many free minifying tools available and some integrate with visual studio. YUI Compressor is a nice online tool that has worked well for me.

Nate Bunton

Nate Bunton is a Lead Software Engineer at Meta Payment Systems. He has over 10 years developing software and leading teams. He works with the Microsoft technology stack focusing in ASP.NET, MVC, HTML, JavaScript, CSS and Web Security. Nate also focuses on Application Lifecycle Management (ALM) engaging his teams with key stakeholders using a variety of agile principals. At Meta Payment Systems, Nate has been a leader in driving technology in the enterprise from new Web Technology to Service Oriented Architecture using WCF and NServiceBus. Nate’s greatest passion is for User Experience, Web Technology and Engaging Teams. He is driven by his desire for continuous learning & improvement.

Posted in Web Development Tagged with: , , , , , ,
0 comments on “JSON.stringify support for IE7
1 Pings/Trackbacks for "JSON.stringify support for IE7"
  1. […] previously posted JSON.stringify support for IE7.  The post describes issues with IE7 support for JSON.stringify.  I recently came across another […]