2005
Ajax Tutorial – Part I
Sponsored Links
XMLHttpRequest
To create a new XMLHttpRequest object is the same like creating any other object:
[js]var httpReq = new XMLHttpRequest();[/js]
above code works with Mozilla and Safari. For Internet Explorer, use the following code:
[js]var httpReq = new ActiveXObject(”Microsoft.XMLHTTP”);[/js]
The following methods work with all three browsers above:
- abort() –> stop request
- getAllResponseHeader() –> return headers set in string format
- getResponseHeader(”headerLabel”) –> return header value for specified header label
- open(”method”,”URL”[,asyncFlag[, "userName"[, "password"]]]) –> prepare data request, with destination address, request method (POST or GET), and additional request attributes. asyncFlag parameter is used to control whether the request is processed asynchronously (true) or synchronously (false).
- send(content) –> do the request.
- setRequestHeader(”label”,”value”) –> set value to speficied header label
Here’s a sample implementation:
[js]var req;
function loadDoc(url) {
// Mozilla/Safari
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
req.onreadystatechange = processReqChange;
req.open(”GET”, url, true);
req.send(null);
// IE
} else if (window.ActiveXObject) {
req = new ActiveXObject(”Microsoft.XMLHTTP”);
if (req) {
req.onreadystatechange = processReqChange;
req.open(”GET”, url, true);
req.send();
}
}
}[/js]
Look on the new term, onreadystatechange attribute. This attribute is used to control what event handler should be call when there’s a new status change on the request process. The status itself is saved on the readyState attribute.
Check the piece of code below:
[js]function processReqChange() {
if (req.readyState == 4) {
//do processing here
}
}[/js]
There are 5 possible values for readyState attribute, from 0 to 4, which mean (in ascending order): uninitialized, loading, loaded, interactive, and complete. Usually, we only need to handle the Loading status and Complete status. Other attributes for XMLHttpRequest object:
- responseText –> request result in string format
- responseXML –> request result in DOM/XML format
- status –> returned code from the server (404 – file not found, 500 – server error, etc)
- statusText –> returned status from the server
Ok, enough with the theory
) In the next part of this tutorial, we are going to make a simple web application using Ajax approach. Later
Pages: 1 2
Ajax Tutorial – Part I is written by cosa and posted under Article, Programming , ajax, Programming, tutorial, xml. If you like it, you might consider subscribing to our feed, follows us on Twitter, or receive our latest posts via email. Or else, you could also or store it to your favourite social bookmark sites. Further information about this article can be found.
And while you're here, why don't you check out our other articles:
Pssst! Most people are coming to this page searching for: ajax tutorial download,ajax tutorials downloads,Ajax loading message ,ajax processing message,ajax tutorials download,download ajax tutorials,ajax messenger tutorial,AJAX TUTORIAL DOWNLOAD FORM,loading message ajax,gmaps ajax,download ajax tutorial,download ajax example,XMLHttprequest iphone tutorial,ajax page loading message,tutorial,loading message + ajax,ajax tutorial news,Ajax Call + req.open("GET", url, true); + turotial,tutorial on ajax status while loading,ajax irc tutorial, 


[...] also wrote several tutorials on May, such as Smarty tutorial (part 1, 2, and 3), and Ajax tutorial (part 1 and [...]