JavaScript
  Home arrow JavaScript arrow AJAX with JSON
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

AJAX with JSON
By: Jayaram Krishnaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 6
    2007-03-12

    Table of Contents:
  • AJAX with JSON
  • Retrieving JSON Text using AJAX
  • The XMLHTTP Request to retrieve JSON
  • Output formatting

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    AJAX with JSON


    (Page 1 of 4 )

    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.

    Add to this the fact that it carries the same message in a smaller size and you will realize the impact it will have on transporting the data. As far as structure and usage are concerned both have similar characteristics, including the response to XMLHttpRequest which has made AJAX so popular. This article will demonstrate retrieving JSON text using AJAX and retrieving values by evaluating the returned text.

    Retrieving XML using AJAX

    Here is the summary and results from the article XML Responses and  AJAX. Herein an AJAX call is made to the URL to get the contents of the webstudents.xml which is shown here.

     <wclass>
    <!--My students who took web programming class with me-->
    <student id="1">
    <name>Linda Jones</name> 
    <legacySkill>Access, VB5.0</legacySkill>
    </student>
    <student id="2">
    <name>Adam Davidson</name> 
    <legacySkill>Cobol, MainFrame</legacySkill>
    </student>
    <student id="3">
    <name>Charles Boyer</name> 
    <legacySkill>HTML, Photoshop</legacySkill>
    </student>
    </wclass> 

    The AJAX call was made using the following code. This is just vanilla AJAX without the use of any other libraries.

    <HTML>
    <HEAD>
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    <TITLE></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) { //alert(xhr.responseText); // document.getElementById("x1").innerHTML=xhr.responseText; document.getElementById("x1").innerHTML= xhr.responseXML. getElementsByTagName('student')[0].firstChild.xml //responseXML document's firstChild and lastChild document.getElementById("x2").innerHTML= xhr.responseXML. documentElement.firstChild.nodeName document.getElementById("x3").innerHTML= xhr.responseXML. documentElement.lastChild.nodeName //responseXML's firstChild and lastChild document.getElementById("x4").innerHTML= xhr.responseXML. firstChild.nodeName document.getElementById("x5").innerHTML=xhr.responseXML. lastChild.nodeName //documentElement's Children property, second child document.getElementById("x6").innerHTML= xhr.responseXML. documentElement.childNodes.item(3).text document.getElementById("x7").innerHTML= xhr.responseXML. documentElement.childNodes.item(3).attributes[0].name document.getElementById("x8").innerHTML= xhr.responseXML. documentElement.childNodes.item(3).attributes[0].value //accessing student nodes as a collection var nodes=xhr.responseXML.getElementsByTagName ("student"); document.getElementById(9).innerHTML=nodes.length; for (i=0; i<nodes.length; i++) { document.getElementById(10+i).innerHTML= nodes.item(i).text; } alert (xhr.responseXML.xml) } else { alert ('The request could not be fulfilled.'); } } } //--> </SCRIPT> </HEAD> <BODY> <INPUT id=button2 onclick= "getPage ('http://xphtek/TestXMLHttp/WebClass.xml')" type=button value="Get the XML Document" name=button2> <P> </P> <div id=x1></div><div id=x2></div> <div id=x3></div><div id=x4></div> <div id=x5></div><div id=x6></div> <div id=x7></div><div id=x8></div> <div id=9></div><div id=10></div> <div id=11></div><div id=12></div> <div id=13></div> </BODY> </HTML>

    Once the document is retrieved it is processed using the XMLDOM API and the ECMA script. The ECMA script retrieves the DOM elements and inserts them into the <div/> elements of the HTML page. In the next section we will see how it is done using the JSON subset.

    More JavaScript Articles
    More By Jayaram Krishnaswamy


       · As noted in the conclusion JSON has certain advantages over XML. It also has...
       · You can also try this very simple example of using json...
     

    JAVASCRIPT ARTICLES

    - Building Dynamic Shadows with JavaScript and...
    - Active Client Pages: Chrys`s Approach
    - The Script Approach to Active Client Pages: ...
    - Principles of Active Client Pages: the Scrip...
    - Active Client Pages: the Script Approach
    - Building an RTF-capable Form with the Ext JS...
    - Creating a Multi-Tabbed Online Form with the...
    - jQuery Overview
    - Constructing a Multi-Column Online Form with...
    - Grouping Field Sets on Dynamic Web Forms wit...
    - Building Dynamic Web Forms with the Ext JS F...
    - More on JavaScript Array Objects
    - Methods of the DOM Location Object
    - The DOM Location Object Properties
    - Handling Remote Files with JavaScript Click ...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT