Home arrow JavaScript arrow Page 3 - AJAX with JSON
JAVASCRIPT

AJAX with JSON


In my previous article I discussed some of the basics of JSON, the JavaScript Object Notation. It is an object literal notation well suited for data exchange. The object and arrays of JSON are very similar to real world data representation in many programming languages and this makes it a natural competitor with the more ubiquitous XML.

Author Info:
By: Jayaram Krishnaswamy
Rating: 4 stars4 stars4 stars4 stars4 stars / 20
March 12, 2007
TABLE OF CONTENTS:
  1. · AJAX with JSON
  2. · Retrieving JSON Text using AJAX
  3. · The XMLHTTP Request to retrieve JSON
  4. · Output formatting

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
AJAX with JSON - The XMLHTTP Request to retrieve JSON
(Page 3 of 4 )

The boiler plate portion of AJAX which consists of making the XMLHttpRequest is exactly the same as shown in the partially colored portion of the next listing. However the AJAX call is now made to get a JSONText file, JSONWebStudents.txt described earlier from the web server (localhost).

Listing of AjaxNewJson.htm:

<HTML>
<HEAD>
<TITLE>AJAX with JSON</TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
var xhr = false;
function getPage (url) {
xhr = false;
//this is the Microsoft browser compatible instantiation
//of XmlHttpRequest
xhr = new ActiveXObject("Msxml2.XMLHTTP");
if (!xhr) {
alert ('XMLHttp failed to instantiate');
return false;
}
xhr.onreadystatechange = statusCheck;
xhr.open ('GET', url, true);
xhr.send (null);
}
function statusCheck() {
if (xhr.readyState == 4) {  if (xhr.status == 200) {  
//document.write(xhr.responseText); alert("Status OK");
var jsonT = eval('('+ xhr.responseText +')');
document.write ("jsonT.wclass.length= " + jsonT.wclass.length); document.write("<br/>"); document.write("jsonT.wclass[0].name"+ jsonT.wclass[0].name); } } } </script> </head> <BODY> <INPUT type="button" id=button2 onclick="getPage ('http://localhost/JSONWebStudents.txt')" value="Get JSON" /> </BODY> </HTML>

The response from the server xhr with its responseText property is evaluated using the JavaScript eval() function which evaluates whatever is given as its argument. It converts the JSON Text to a JSON object (jsonT). With the object in the hand it is easy to recover the values using the dot notation as shown in the two document.write() statements that follow the eval(). The resulting display when the above file is browsed is shown in the next picture. It will be helpful to read the earlier tutorial JSON Basics.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks
- Dynamic jQuery Styling

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials