Home arrow JavaScript arrow Page 2 - Parsing AJAX Responses with JavaScript and the innerHTML Property
JAVASCRIPT

Parsing AJAX Responses with JavaScript and the innerHTML Property


If you work with AJAX and have ever wondered which approach is best to use when parsing the responses triggered by a web server after performing an HTTP request, this article series is for you. Composed of three parts, it will lay out your options and the most efficient approaches. This article, the first part of the series, focuses on the "responseText" property and the "innerHTML" property.

Author Info:
By: Alejandro Gervasio
Rating: 3 stars3 stars3 stars3 stars3 stars / 8
October 30, 2007
TABLE OF CONTENTS:
  1. · Parsing AJAX Responses with JavaScript and the innerHTML Property
  2. · Working with HTTP XML Request objects
  3. · Parsing web server responses
  4. · The full source code of the sample AJAX application

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Parsing AJAX Responses with JavaScript and the innerHTML Property - Working with HTTP XML Request objects
(Page 2 of 4 )

In order to demonstrate how to parse web server responses via the popular "responseText" property of AJAX, I'm first going to define a short JavaScript function. It will be tasked with sending HTTP requests via an XML HTTP Request object to a specified URL on the server.

Having said that, here's the complete definition for this brand new function:

function sendHttpRequest(url,callbackFunc,respXml){

var xmlobj=null;

try{

  xmlobj=new XMLHttpRequest();

}

catch(e){

  try{

    xmlobj=new ActiveXObject("Microsoft.XMLHTTP");

}

catch(e){

  alert('AJAX is not supported by your browser!');

   return false;

  }

 }

xmlobj.onreadystatechange=function(){

  if(xmlobj.readyState==4){

  if(xmlobj.status==200){

respXml?eval(callbackFunc+'(xmlobj.responseXML)'):eval
(callbackFunc+'(xmlobj.responseText)');

  }

 }

}

// open socket connection

  xmlobj.open('GET',url,true);

// send http header

xmlobj.setRequestHeader('Content-Type','plain/text; charset=UTF-
8');

// send http request

  xmlobj.send(null);

 }

The signature that corresponds to the above function should be familiar to you, since I already used in some of my previous articles on AJAX. As you can see, the prior "sendHttpRequest()" JavaScript function accepts a few straightforward input arguments, such as the name of the file to fetch on the server, and then the name of the callback function that will be invoked when the request has been successfully triggered, and finally a Boolean flag that indicates whether or not the respective server response should be evaluated as XML. Quite simple to grasp, right?

Okay, so I built a JavaScript function that's capable of performing an HTTP request to a given web server. What's next? Good question. Assuming that you had no major problems understanding how the previous JavaScript function works, it's time to develop a basic hands-on example. In this example, some database records, previously fetched from a sample MySQL table via AJAX, will be parsed properly in the client by using the "innerHTML" JavaScript property.

To see how this example will be created, click on the link below and keep reading.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- 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

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 3 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials