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 
Sun Developer Network 
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 / 4
    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...
     

    JAVASCRIPT ARTICLES

    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






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