JavaScript
  Home arrow JavaScript arrow Page 3 - 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: 4 stars4 stars4 stars4 stars4 stars / 9
    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 - 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.

    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...
       · as i was going from step to step, clicking my way through this tutorial, I was like,...
     

    JAVASCRIPT ARTICLES

    - Using jQuery to Preload Images with CSS and ...
    - Using Client-Side Scripting to Preload Image...
    - Removing Non-Semantic Markup when Preloading...
    - Using the Display CSS Property to Preload Im...
    - Preloading Images with CSS and JavaScript
    - Scaling and Moving Web Page Elements with th...
    - Fading, Hiding and Sliding HTML Elements wit...
    - Toggling CSS Properties with the GX JavaScri...
    - Cancel, Queue and Pause Animations with the ...
    - Producing Elastic Effects with the GX JavaSc...
    - Moving Divs Diagonally with the GX JavaScrip...
    - Moving Elements Vertically and Horizontally ...
    - Making Bouncing Effects in Parallel with the...
    - Creating Bouncing Effects with the GX JavaSc...
    - Manipulating Background Colors with the GX J...







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 11 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek