June 18, 2008

Part1: HTTPRequest - setting up the object

Category: Ajax, Technologies — Corina @ 11:14 pm

Ajax (asynchronous javascript and XML) is a group of inter-related web dev techniques used for creating web applications. Few major benefits are: usability, separation of data, format, style and function; request and send data to the server without reloading the page, by using http requests.
If you want to know how Ajax become part of the web, checkout this extremelly interesting post, The birth of AJAX - an amazing story.
The XMLHttpRequest object is supported by Internet Explorer 5.0+, Safari 1.2, Mozilla 1.0 / Firefox, Opera 8+, and Netscape 7.
Create the object

function getXMLHttpObject()

{
var xmlHttp;
try{//mozzila, firefox, opera
xmlHttp = new XMLHttpRequest();
}
catch(e){//ie6+
try{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e){
try{//ie. 5
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
alert("your browser does not support ajax");
return false;
}
}
}
return xmlHttp;
}

xmlHttp = new getXMLHttpObject();

The XMLHTTPRequest object has
Methods:

  • xmlHttp.abort() - abort/cancel all activity and reset the object
  • xmlHttp.getAllResponseHeaders() - returns all headers in a single string
  • xmlHttp.getResponseHeader(header) - takes as param an http header (string) and returns the value of the given header
  • xmlHttp.open(method, uri, [assync flag, username, password]) - method - GET/POST, uri , flag - true or false [true if omited], username and password null if omited; this method is calling the server and does not return any value
  • xmlHttp.send(param) - data to be transmited to the server; xmlHttp.send() same as invoked with null xmlHttp.send(null); does not return a value; if send is invoked with parameters, the content type has to be set up before via setRequestHeader
  • xmlHttp.setRequestHeader(header, value)
    - create or change a header

Attributes::

  • xmlHttp.onreadystatechange - of type Event Handler
  • xmlHttp.readyState - the status of the request
    • 0 = uninitialized
    • 1 = loading
    • 2 = loaded
    • 3 = interactive
    • 4 = complete
  • xmlHttp.responseText - server’s response as a string; the ready state value has to be 3 or 4
  • xmlHttp.responseXML - dom compatible xml data; ready state must be 4[otherwise is null]
  • xmlHttp.status - numaric value of the server status [i.e. 404]
  • xmlHttp.StatusText - text value of the server status [i.e. not found]

    No Comments »

    No comments yet.

    RSS feed for comments on this post. | TrackBack URI

    Leave a comment

    XHTML ( You can use these tags):
    <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .