Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: A Simpler Ajax Path

The example returns the data to the browser in a simple DSV (delimiter-separated values) format.

Tutorial Details:

Preparing Form Data to POST
Other resources online covering the XMLHttpRequest object demonstrate how to use it with simple GET requests. The object is also capable of doing POSTs, which makes it a much more useful tool for creating web applications.

To use the POST method, pass data to the XMLHttpRequest object in query-string format (for example, ArtistName=Kolida&SongName=Wishes), which the object then sends to the server just like a normal form POST. You can use JavaScript to pull data a piece at a time out of web-form elements, and then format the data into a query string. If I wanted to do this for my playlist search function, I could use something like this:

var searchForm = document.forms['searchForm'];
var artistName = searchForm.ArtistName.value;
var songName = searchForm.SongName.value;
var queryString = '?ArtistName=' + escape(artistName) + '&SongName=' +
escape(songName);
Note: Be sure to escape (URL encode) the values.

Of course, keeping in mind that one of Larry Wall's three great virtues of a programmer is laziness (and in that respect I'm really virtuous), I wrote a general function to automate this process, to put all the data from a form into a query string. This function allows me to POST form data with the XMLHttpRequest object without having to do a lot of extra work every time. That makes it significantly easier to integrate the use of the object with my existing application code.

Creating the Object
To create the object in JavaScript for IE, use new ActiveXObject("Microsoft.XMLHTTP"). In Mozilla/Firefox and Safari, use new XMLHttpRequest(). The first half of a simple function to create and use the object looks like this:

function xmlhttpPost(strURL, strSubmit, strResultFunc) {

var xmlHttpReq = false;

// Mozilla/Safari
if (window.XMLHttpRequest) {
xmlHttpReq = new XMLHttpRequest();
xmlHttpReq.overrideMimeType('text/xml');
}
// IE
else if (window.ActiveXObject) {
xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
To make this a reusable, generic function, I pass in three parameters: the URL for the processing page on the server, the query-string formatted data to submit, and the name of the JavaScript function that will process the response from the server (to invoke later through eval).

Note the addition of the overrideMimeType method call in the Mozilla/Safari code. Without this, some people have reported that some versions of Mozilla lock up when the server returns anything other than XML. (I cannot confirm this issue, as I have not experienced this problem myself.)

Also, if you want to support older browsers, you can then test the xmlHttpReq variable and fall back to other methods of submitting data if the object is not present.


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
A Simpler Ajax Path

View Tutorial:
A Simpler Ajax Path

Related Tutorials:

Create a custom Java 1.2-style ClassLoader - JavaWorld March 2000
Create a custom Java 1.2-style ClassLoader - JavaWorld March 2000
 
Code generation using Javadoc - JavaWorld August 2000
Code generation using Javadoc - JavaWorld August 2000
 
XML document processing in Java using XPath and XSLT - JavaWorld September 2000
XML document processing in Java using XPath and XSLT - JavaWorld September 2000
 
Mapping XML to Java, Part 2 - JavaWorld October 2000
Mapping XML to Java, Part 2 - JavaWorld October 2000
 
Eliminate tedious programming: Recover data with XML and Reflection - JavaWorld November 2000
Eliminate tedious programming: Recover data with XML and Reflection - JavaWorld November 2000
 
Untangle your servlet code with reflection - JavaWorld December 2000
Untangle your servlet code with reflection - JavaWorld December 2000
 
JUnit best practices - JavaWorld December 2000
JUnit best practices - JavaWorld December 2000
 
Get the app out - JavaWorld January 2001
Get the app out - JavaWorld January 2001
 
J2ME: The next major games platform? - JavaWorld March 2001
J2ME: The next major games platform? - JavaWorld March 2001
 
Untangle your servlet code with reflection
Untangle your servlet code with reflection
 
Axis: The next generation of Apache SOAP
Axis: The next generation of Apache SOAP
 
Quickly access files and directories you use repeatedly
Quickly access files and directories you use repeatedly
 
J2ME app development on the 6600
J2ME app development on the 6600 Introduction When you first look at the Nokia 6600, the first reaction is very predictable. wow! It is truely a good looking phone. A stunner. Let's find out if there's more to this phone than just good looks.
 
Commons Launcher
The Launcher Component is designed to be a cross platform Java application launcher.
 
Euler proof mechanism
Euler is an inference engine supporting logic based proofs of test cases (*).
 
From Writing Programs to Creating Compilers
From Writing Programs to Creating Compilers In this article we build a simple compiler that augments Java with tasks (independent blocks of code that execute in parallel), thus creating a new language called AJ that well supports the programming of syste
 
Leverage JNLP and SOAP for Java Thick-client Development
Leverage JNLP and SOAP for Java Thick-client Development The hype during the mid-to-late 1990's over Java's utility to run swarms of autonomous applets was greatly exaggerated. This early enthusiasm (and marketing) for Java as a language with which devel
 
Enhance looping in Java 5.0 with for/in
The for/in loop -- often called either enhanced for or foreach is largely a convenience feature in Java 5.0. It doesn\\'t really offer any new functionality, but certainly makes several routine coding tasks simpler.
 
Parsing an XML Document with XPath
The getter methods in the org.w3c.dom package API are commonly used to parse an XML document. But J2SE 5.0 also provides the javax.xml.xpath package to parse an XML document with the XML Path Language (XPath) .
 
Creating EJB clients using the Eclipse Rich Client Platform
This article shows how to build a sample EJB client using the Eclipse Rich Client Platform (RCP), which has become increasingly popularity due to its extensible nature.
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.