Jump to content

XMLHttpRequest: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Spankman (talk | contribs)
Moved to tutorials.
Spankman (talk | contribs)
Not a browser.
Line 6: Line 6:


==History and support==
==History and support==
The XMLHTTP object was originally developed by [[Microsoft]] Corp., available since [[Internet Explorer]] 5.0 as an [[ActiveX]] object, via JScript, VBScript, or other scripting languages supported by the browser. Mozilla contributors then implemented a compatible native version called XMLHttpRequest in [[Mozilla Application Suite|Mozilla]] 1.0. This implementation was later followed by [[Apple Computer|Apple]] since [[Safari (web browser)|Safari]] 1.2, [[Opera Software]] since [[Opera (web browser)|Opera]] 8.0 and [[iCab]] since 3.0b352. Additionally, [[Open Laszlo]] added support in version 3.1.
The XMLHTTP object was originally developed by [[Microsoft]] Corp., available since [[Internet Explorer]] 5.0 as an [[ActiveX]] object, via JScript, VBScript, or other scripting languages supported by the browser. Mozilla contributors then implemented a compatible native version called XMLHttpRequest in [[Mozilla Application Suite|Mozilla]] 1.0. This implementation was later followed by [[Apple Computer|Apple]] since [[Safari (web browser)|Safari]] 1.2, [[Opera Software]] since [[Opera (web browser)|Opera]] 8.0 and [[iCab]] since 3.0b352.


Most well-designed web pages which use XMLHTTP are designed in such a way that the minor differences in these XMLHTTP object implementations are made irrelevant by encapsulating the invocation of the XMLHTTP object in a simple [[JavaScript]] wrapper, or by using one of the many frameworks available.
Most well-designed web pages which use XMLHTTP are designed in such a way that the minor differences in these XMLHTTP object implementations are made irrelevant by encapsulating the invocation of the XMLHTTP object in a simple [[JavaScript]] wrapper, or by using one of the many frameworks available.

Revision as of 06:44, 29 April 2006

XMLHTTP is a set of APIs 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 Client-Side and Server-Side.

XMLHTTP 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 XMLHTTP applications include Google's Gmail service, Google Suggest dynamic lookup interface, MSN's Virtual Earth and the MapQuest dynamic map interface.

The data returned from an XMLHTTP request will often be provided by executing back-end database queries. Besides XML, XMLHTTP can be used to fetch data in other formats, e.g. JSON or even plain text.

History and support

The XMLHTTP object was originally developed by Microsoft Corp., available since Internet Explorer 5.0 as an ActiveX object, via JScript, VBScript, or other scripting languages supported by the browser. Mozilla contributors then implemented a compatible native version called XMLHttpRequest in Mozilla 1.0. This implementation was later followed by Apple since Safari 1.2, Opera Software since Opera 8.0 and iCab since 3.0b352.

Most well-designed web pages which use XMLHTTP are designed in such a way that the minor differences in these XMLHTTP object implementations are made irrelevant by encapsulating the invocation of the XMLHTTP object in a simple JavaScript wrapper, or by using one of the many frameworks available.

Traditionally, there have been other methods to achieve a similar effect of server dynamic applications using scripting languages and/or plugin technology:

Known problems

Microsoft Internet Explorer Cache issues

Internet Explorer implements caching for GET requests. Authors who are not familiar with HTTP caching expect GET requests not to be cached, or for the cache to be avoided as with the refresh button. In some situations, failing to circumvent caching is a bug. One solution to this is to use the POST request method, which is never cached; however, it is intended for non-idempotent operations. A solution using the GET request method is to include a unique (unprecedented) querystring with each call, as shown in the example below.

req.open("GET", "xmlprovider.php?hash=" + Math.random());

or set the Expires header to an old date in your script that generates the XML content. For PHP that would be

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" );

For Java Servlets that would be


response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "must-revalidate");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Cache-Control", "no-store");
response.setDateHeader("Expires", 0);  


Alternatively, force the XMLHTTPRequest object to retrieve the content anyway by including this in the request:

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 massive reduction in performance.

Accents and Non-ASCII characters problem

If the server answer does not encapsulate the result in an XML format, the 'responseText' will not work correctly if you use accents like 'é' or other non ASCII characters. In that latter case, everything will work fine on Firefox and on your first request with Internet Explorer (you might have minor display problem). But if you ask a second time, IE will use the cache result and generate a JavaScript error.

XML formatted results and the use of the slightly more complex 'responseXML' method will work with any UTF-8 characters.

See also

Documentation/Browser implementations

Tutorials