javascript xmlhttprequest

XMLHttpRequest sends a request to a URL. The URL page can be a file at the server side, and may return data on the server for further processing. It's very important in some applications such as AJAX.

var req;
if (window.XMLHttpRequest){
req = new XMLHttpRequest();
}
else if (window.ActiveXObject){ //IE Browser
req = new ActiveXObject("Microsoft.XMLHTTP");
}

The get property of open() method requires some data from a URL page, and then process the return data.
req.open("GET", 'test.php?q=id', true);
req.onreadystatechange = handlereq;
req.send(null);
function handlereq()
{
if (req.readyState == 4)
{
var str = req.responseText;
document.write(str)
}
}

The put property of open() method sends data to a URL page, and do not need a response.
req.open("PUT", 'test.php?q=id', true);
req.send(null);

If the 3rd argument of the open() method is true, the process is handled synchronously, otherwise synchronously.


List of XMLHttpRequest response attributes: AttributeFunction statusreturn the HTTP status code. Error states include UNSENT, OPENED, or error flag, return 0 and terminate the process. statusTextreturn HTTP status text, if error states, return '' and terminate the process responseTypearraybuffer, blob, document, json, text responsethe response entity body responseTextthe response text entity body responseXMLdocument response entity body