XMLHttpRequest (XHR) is an API that can be used by JavaScript, JScript, VBScript and other web browser scripting languages to transfer and manipulate XML data to and from a web server using HTTP, establishing an independent connection channel between a web page's Client-Side and Server-Side.
The data returned from XMLHttpRequest calls will often be provided by back-end databases. Besides XML, XMLHttpRequest can be used to fetch data in other formats, e.g. JSON or even plain text.
XMLHttpRequest is an important part of the Ajax web development technique, and it is used by many websites to implement responsive and dynamic web applications. Examples of XMLHttpRequest applications include Google's Gmail service, Google Suggest dynamic look-up interface, Meebo, MSN's Virtual Earth and the MapQuest dynamic map interface.
| Method | Description |
|---|---|
| abort() | Cancels the current request. |
| getAllResponseHeaders() | Returns the complete set of HTTP headers as a string. |
| getResponseHeader( headerName ) | Returns the value of the specified HTTP header. |
| open( method, URL ) open( method, URL, async ) open( method, URL, async, userName ) open( method, URL, async, userName, password ) | Specifies the method, URL, and other optional attributes of a request. |
| send( content ) | Sends the request. |
| setRequestHeader( label, value ) | Adds a label/value pair to the HTTP header to be sent. |
| Property | Description |
|---|---|
| onreadystatechange | An event handler for an event that fires at every state change. |
| readyState | Returns the state of the object as follows: | 0 = uninitialized, 1 = open, 2 = sent, 3 = receiving and 4 = loaded.
| responseText | Returns the response as a string. |
| responseXML | Returns the response as XML. This property returns an XML document object, which can be examined and parsed using W3C DOM node tree methods and properties. |
| status | Returns the status as a number (e.g. 404 for "Not Found" and 200 for "OK"). |
| statusText | Returns the status as a string (e.g. "Not Found" or "OK"). |
The Mozilla project incorporated the first compatible native implementation of XMLHttpRequest in Mozilla 1.0 in 2002. This implementation was later followed by Apple since Safari 1.2, Konqueror, Opera Software since Opera 8.0 and iCab since 3.0b352.
The World Wide Web Consortium published a Working Draft specification for the XMLHttpRequest object's API on 5 April 2006*. While this is still a work in progress, its goal is "to document a minimum set of interoperable features based on existing implementations, allowing Web developers to use these features without platform-specific code". The draft specification is based upon existing popular implementations, to help improve and ensure interoperability of code across web platforms.
Web pages that use XMLHttpRequest or XMLHTTP can mitigate the current minor differences in the implementations either by encapsulating the XMLHttpRequest object in a JavaScript wrapper, or by using an existing framework that does so. In either case, the wrapper should detect the abilities of current implementation and work within its requirements.
Traditionally, there have been other methods to achieve a similar effect of server dynamic applications using scripting languages and/or plugin technology:
req.open("GET", "xmlprovider.php?hash=" + Math.random());
Setting the "Expires" header to reference a date in the past will also avoid caching of the response. Here is an example in PHP.
header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); // disable IE caching header( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" ); header( "Cache-Control: no-cache, must-revalidate" ); header( "Pragma: no-cache" );
Caching may also be disabled, as in this Java Servlet example.
response.setHeader( "Pragma", "no-cache" ); response.addHeader( "Cache-Control", "must-revalidate" ); response.addHeader( "Cache-Control", "no-cache" ); response.addHeader( "Cache-Control", "no-store" ); response.setDateHeader("Expires", 0);
Alternatively, it is possible to force the XMLHttpRequest object to retrieve the content anyway, as shown in this example.
req.open( "GET", "xmlprovider.php" ); req.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" ); req.send( null );
It is important to note that these techniques should be used only if the caching is inappropriate. Generally it is better to obtain the performance benefits of caching, perhaps combined with explicitly detailing modification dates or other relevant headers on the server so as to get as much caching as possible without it causing poor results.
Many programmers use the techniques above indiscriminately, which can result in a significant reduction in performance.
XML formatted results and the use of a slightly more complex 'responseXML' method will work with any UTF-8 characters.
JavaScript programming language | XML | HTTP | HTTP clients | Ajax | XMLHttpRequest | XMLHttpRequest | XMLHttpRequest | XMLHttpRequest | XMLHTTP | XMLHttpRequest | XMLHTTP | XMLHttpRequest | XMLHttpRequest | XMLHttpRequest | XMLHTTP
This article is licensed under the GNU Free Documentation License.
It uses material from the
"XMLHttpRequest".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world