MozillaZine

XMLHttpRequest

From MozillaZine Knowledge Base

(Difference between revisions)
Revision as of 00:47, 21 September 2005
Hudsmyge (Talk | contribs)
(header update)
<-- Previous diff
Revision as of 09:27, 21 September 2005
Asqueella (Talk | contribs)
(responseXML and overrideMimeType() - formatting)
Next diff -->
Line 39: Line 39:
===responseXML and overrideMimeType()=== ===responseXML and overrideMimeType()===
-If you load an XML document, the responseXML property will contain the document as an XmlDocument object that you can manipulate using DOM methods. For loaded non-XML documents (whose Content-Type header does not indicate XML content), the responseXML property is null. If you know in advance that the document is valid XML, you can remedy and override a broken Content-Type header, by invoking overrideMimeType( 'text/xml' ) (perhaps tucking on '; charset=iso-8859-1' or similar, as needed) before the send() call.+If you load an XML document, the <code>responseXML</code> property will contain the document as an [http://xulplanet.com/references/objref/XMLDocument.html XMLDocument] object that you can manipulate using DOM methods. For loaded non-XML documents (whose Content-Type header does not indicate XML content), the <code>responseXML</code> property is <code>null</code>. If you know in advance that the document is valid XML, you can remedy and override a broken Content-Type header, by invoking <code>overrideMimeType('text/xml')</code> (perhaps tucking on <code>'; charset=iso-8859-1'</code> or similar, as needed) before the <code>send()</code> call.
-<pre>+ var req = new XMLHttpRequest();
-var req = new XMLHttpRequest();+ <nowiki>req.open('GET', 'http://www.example.com/really-xml.txt', false);</nowiki>
-req.open( 'GET', 'http://www.example.com/really-xml.txt', false );+ '''req.overrideMimeType('text/xml');'''
-req.overrideMimeType( 'text/xml' );+ req.send(null);
-req.send( null );+ alert(req.responseXML);
-alert( req.responseXML );+
-</pre>+
===setRequestHeader()=== ===setRequestHeader()===

Revision as of 09:27, 21 September 2005

This page is part of the extension development documentation project.

Ask your questions in MozillaZine Forums. Also try browsing example code.

Note: development documentation is in process of being moved to Mozilla Development Center (MDC).

XMLHttpRequest is a JavaScript object that was created by Microsoft and adopted by Mozilla. You can use it to easily retrieve data via HTTP. Despite its name, it can be used for more than just XML documents.

Contents

Basic Usage

Using XMLHttpRequest is very simple. You create an instance of the object, open a URL, and send the request. The HTTP status code of the result, as well as the result document are available in the request object afterwards.

Example

req = new XMLHttpRequest();
req.open('GET', 'http://www.mozilla.org/', false); 
req.send(null);
if(req.status == 200)
  dump(req.responseText);

Note that this example works synchronously, so it will block the user interface if you call this from your javascript. You should not use this in practice.

Asynchronous Usage

If you intend to use XMLHttpRequest from an extension, you should let it load asynchronously. In asynchronous usage, you get a callback when the data has been received, which lets the browser continue to work as normal while your request is happening.

Example

req = new XMLHttpRequest();
req.open('GET', 'http://www.mozilla.org/', true);
req.onreadystatechange = function (aEvt) {
  if (req.readyState == 4) {
     if(req.status == 200)
      dump(req.responseText);
     else
      dump("Error loading page\n");
  }
};
req.send(null); 

Other Properties and Methods

In addition to the properties and methods shown above, there are other useful properties and methods on the request object.

responseXML and overrideMimeType()

If you load an XML document, the responseXML property will contain the document as an XMLDocument object that you can manipulate using DOM methods. For loaded non-XML documents (whose Content-Type header does not indicate XML content), the responseXML property is null. If you know in advance that the document is valid XML, you can remedy and override a broken Content-Type header, by invoking overrideMimeType('text/xml') (perhaps tucking on '; charset=iso-8859-1' or similar, as needed) before the send() call.

var req = new XMLHttpRequest();
req.open('GET', 'http://www.example.com/really-xml.txt', false);
req.overrideMimeType('text/xml');
req.send(null);
alert(req.responseXML);

setRequestHeader()

This method can be used to set an HTTP header on the request before you send it.

req = new XMLHttpRequest();
req.open('GET', 'http://www.mozilla.org/', true);
req.setRequestHeader("X-Foo", "Bar");
req.send(null);

getResponseHeader()

This method can be used to get an HTTP header from the server response.

req = new XMLHttpRequest();
req.open('GET', 'http://www.mozilla.org/', true);
req.send(null);
dump("Content-Type: " + req.getResponseHeader("Content-Type") + "\n");

References

  1. XULPlanet documentation
  2. Microsoft documentation
  3. "Using the XMLHttpRequest Object" (jibbering.com)
  4. Sarissa: Sarissa is a cross-browser ECMAScript library for client side XML manipulation, including loading XML from URLs or strings, performing XSLT transformations, XPath queries and more. Supported: Gecko (Mozilla, Firefox etc), IE, KHTML (Konqueror, Safari). If you're writing JavaScript that is used in both XUL applications and HTML pages, and the HTML pages may be viewed in non-Gecko-based applications (such as Internet Explorer, Opera, Konqueror, Safari), you should consider using Sarissa to parse and/or serialize XML. Note: Do not create a DOM object using document.implementation.createDocument() and then use Sarissa classes and methods to manipulate that object. It will not work. You must use Sarissa to create the initial DOM object.