XMLHttpRequest object: is a browser object that allows you to communicate to the server. To create one of these objects you use the following JavaScript code:
var newXHR = new XMLHttpRequest(); // Doesn’t work in IE 6 though
In order to send a GET request using the object above, you do something like:
newXHR.open(‘GET’, ‘shop.php?productID=34’);
To indicate the callback function that will process the request received back from the server:
newXHR.onreadystatechange = myCallbackFunction;
Up to this point, everything has been setup, now to send the data to the server you use:
newXHR.send(null);
If you want to send additional parameters, instead of null you specify:
newXHR.send('q=javascript');
The XHR object receives up to three pieces of information back from the server:
- status: like 404, 500, 200, etc
- responseText property: it is part of the XHR object, and stores the server response, could be HTML, plain text, or a JSON object
- responseXML property: also part of the XHR object. Loaded if the server responds with XML