XMLHTTP

From Wikipedia, the free encyclopedia.

(Redirected from XMLHttpRequest)
Jump to: navigation, search

XMLHTTP is a set of APIs that can be used by JavaScript, JScript, VBScript and other web browser scripting languages to transfer XML or other data to and from a web server using HTTP. The biggest advantage of XMLHTTP is the ability to dynamically update a webpage without reloading the entire webpage or using software plugins (previously possible only using hidden IFRAMEs with DHTML). 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 Virtual Earth and MapQuest dynamic map interface.

XMLHTTP is an important part of the Ajax web development technique.

Besides XML, XMLHTTP can be used to fetch data in other formats, e.g. JSON or even plaintext.

Contents

History and support

The object was originally invented by Microsoft, used since Internet Explorer 5.0 as an ActiveX object, which is hence accessible via JavaScript, VBScript, or other scripting languages supported by the browser. Mozilla contributors then implemented a compatible native version in Mozilla 1.0. This was later followed by Apple since Safari 1.2 and Opera Software since Opera 8.0.

Most well-designed web pages using XMLHTTP are designed to hide the minor differences in these XMLHTTP object implementations by encapsulating the invocation of the XMLHTTP object in a simple JavaScript wrapper that autodetects the browser and executes the appropriate snippet.

Known problems

Microsoft Internet Explorer Cache issues

Internet Explorer implements proper caching for GET requests. Authors not familiar with HTTP caching rules expect GET requests not to be cached. This can result in unexpected bugs with status modifiers set to update after a request is made. Proper solution to this is using POST request method, which is never cached.

An easy workaround is to include a unique querystring with each call as shown in the example below.

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

or set the Expires header to -1 in your script that generates the XML content. For PHP that would be

header('Expires: -1');

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

Safari POST Content-Type

When using Safari to generate an XMLHttpRequest POST to a PHP script you have to send the data as urlencoded by default and available in $_POST

req.open("POST", "xml.php");
req.send("variable=value&var2=value");

If you wish to send other content (like a full XML document) you must set the Content-Type header and the entire content will be available in the $HTTP_RAW_POST_DATA

req.open("POST", "xml.php");
try { // IE does not have this method
   req.setRequestHeader('Content-Type','text/xml');
} catch (error) {};
req.send('<xmldoc><tag val="test"/><value>test</value></xmldoc>');

Accents and Non-ascii characters problem

If the server answer does not encapsulate the result in an XML format, the 'responseText' will work correctly unless you use accents like 'é' or other non ASCII characters. In that later 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 formated results and the use of the slightly more complex 'responseXML' method will work with any UTF-8 characters.

External links

Technical information

Browser implementations/documentation

Tutorials

Examples of applications that use XMLHTTP

Personal tools