The Wayback Machine - https://web.archive.org/web/20100627022404/https://developer.mozilla.org/en/XmlHttpRequest

Mozilla.com


XMLHttpRequest

XMLHttpRequest is a JavaScript object that was designed by Microsoft, adopted by Mozilla, and is now being standardized in the W3C.  It provides an easy way to retrieve data at a URL.  Despite its name, XMLHttpRequest can be used to retrieve any type of data, not just XML, and it supports connections other than HTTP (including file and ftp).

To create an instance of XMLHttpRequest, simply do this:

var req = new XMLHttpRequest();

For details about how to use XMLHttpRequest, see Using XMLHttpRequest.

Note: By default, Firefox 3 limits the number of XMLHttpRequest connections per server to 6 (previous versions limit this to 2 per server).  Some interactive web sites may keep an XMLHttpRequest connection open, so opening multiple sessions to such sites may result in the browser hanging in such a way that the window no longer repaints and controls don't respond.  This value can be changed by editing the network.http.max-persistent-connections-per-server preference in about:config.

Method overview

void abort();
string getAllResponseHeaders();
ACString getResponseHeader(in AUTF8String header);
[noscript] void init(in nsIPrincipal principal, in nsIScriptContext scriptContext, in nsPIDOMWindow ownerWindow);
void open(in AUTF8String method, in AUTF8String url);
[noscript] void openRequest(in AUTF8String method, in AUTF8String url, in boolean async, in AString user, in AString password);
void overrideMimeType(in AUTF8String mimetype);
void send([optional] in nsIVariant body);
void sendAsBinary(in DOMString body); New in Firefox 3
void setRequestHeader(in AUTF8String header, in AUTF8String value);

Properties

Attribute Type Description
channel nsIChannel  The channel used by the object when performing the request.  This is null if the channel hasn't been created yet.  In the case of a multi-part request, this is the initial channel, not the different parts in the multi-part request.  Requires elevated privileges to access; read-only. Non-standard
mozBackgroundRequest boolean

Indicates whether or not the object represents a background service request.  If true, no load group is associated with the request, and security dialogs are prevented from being shown to the user.  Non-standard

In cases in which a security dialog (such as authentication or a bad certificate notification) would normally be shown, the request simply fails instead.

multipart boolean

Indicates whether or not the response is expected to be a stream of possibly multiple XML documents.  If set to true, the content type of the initial response must be multipart/x-mixed-replace or an error will occur.  All requests must be asynchronous.

This enables support for server push; for each XML document that's written to this request, a new XML DOM document is created and the onload handler is called between documents.

Note: When this is set, the onload handler and other event handlers are not reset after the first XML document is loaded, and the onload handler is called after each part of the response is received.

onreadystatechange

nsIDOMEventListener

A JavaScript function object that is called whenever the readyState attribute changes.  The callback is called from the user interface thread.

Warning: This must not be used from native code.  You should also not use this with synchronous requests.
readyState long

The state of the request:

Value State Description
0 UNINITIALIZED open() has not been called yet.
1 LOADING send() has not been called yet.
2 LOADED send() has been called, and headers and status are available.
3 INTERACTIVE Downloading; responseText holds partial data.
4 COMPLETED The operation is complete.
responseText AString The response to the request as text, or null if the request was unsucessful or has not yet been sent.  Read-only.
responseXML nsIDOMDocument

The response to the request as a DOM Document object, or null if the request was unsuccessful, has not yet been sent, or cannot be parsed as XML.  The response is parsed as if it were a text/xml stream.  Read-only.

Note: If the server doesn't apply the text/xml Content-Type header, you can use overrideMimeType() to force XMLHttpRequest to parse it as XML anyway.
status unsigned long The status of the response to the request.  This is the HTTP result code (for example, status is 200 for a successful request).  Read-only.
statusText AUTF8String The response string returned by the HTTP server.  Unlike status, this includes the entire text of the response message ("200 OK", for example).  Read-only.
upload nsIXMLHttpRequestUpload The upload process can be tracked by adding an event listener to upload.  New in Firefox 3.5
withCredentials boolean

Indicates whether or not cross-site Access-Control requests should be made using credentials such as cookies or authorization headers.  New in Firefox 3.5

Note: This never affects same-site requests.

The default is false.

Methods

abort()

Aborts the request if it has already been sent.

void abort();
Parameters

None.

getAllResponseHeaders()

Returns all the response headers as a string.

Note: For multipart requests, this returns the headers from the current part of the request, not from the original channel.
string getAllResponseHeaders();
Parameters

None.

Return value

The text of all response headers, or null if no response has been received.

getResponseHeader()

Returns the text of a specified header.

ACString getResponseHeader(
  in AUTF8String header
);
Parameters
header
The name of the header to retrieve.
Return value

A string containing the text of the specified header, or null if either the response has not yet been received or the header doesn't exist in the response.

init()

Initializes the object for use from C++ code.

Warning: This method must not be called from JavaScript.
[noscript] void init(
  in nsIPrincipal principal,
  in nsIScriptContext scriptContext,
  in nsPIDOMWindow ownerWindow
);
Parameters
principal
The principal to use for the request; must not be null.
scriptContext
The script context to use for the request; must not be null.
ownerWindow
The window associated with the request; may be null.

open()

Initializes a request.  This method is to be used from JavaScript code; to initialize a request from native code, use openRequest() instead.

Note: Calling this method an already active request (one for which open() or openRequest() has already been called) is the equivalent of calling abort().
void open(
  in AUTF8String method,
  in AUTF8String url,
  [optional] in boolean async,
  [optional] in AString user,
  [optional] in AString password
);
Parameters
method
The HTTP method to use; either "POST" or "GET".  Ignored for non-HTTP URLs.
url
The URL to which to send the request.
async
An optional boolean parameter, defaulting to true, indicating whether or not to perform the operation asynchronously.  If this value is false, the send() method does not return until the response is received.  If true, notification of a completed transaction is provided using event listeners.  This must be true if the multipart attribute is true, or an exception will be thrown.
user
The optional user name to use for authentication purposes; by default, this is an empty string.
password
The optional password to use for authentication purposes; by default, this is an empty string.

openRequest()

Initializes a request.  This method is to be used from native code; to initialize a request from JavaScript code, use open() instead.

Note: Calling this method an already active request (one for which open() or openRequest() has already been called) is the equivalent of calling abort().
void open(
  in AUTF8String method,
  in AUTF8String url,
  in boolean async,
  in AString user,
  in AString password
);
Parameters
method
The HTTP method to use; either "POST" or "GET".  Ignored for non-HTTP URLs.
url
The URL to which to send the request.
async
An optional boolean parameter, defaulting to true, indicating whether or not to perform the operation asynchronously.  If this value is false, the send() method does not return until the response is received.  If true, notification of a completed transaction is provided using event listeners.  This must be true if the multipart attribute is true, or an exception will be thrown.
user
The optional user name to use for authentication purposes; by default, this is an empty string.
password
The optional password to use for authentication purposes; by default, this is an empty string.

overrideMimeType()

Overrides the MIME type returned by the server.

Note: This method must be called before send().
void overrideMimeType(
  in AUTF8String mimetype
);
Parameters
mimetype
The type that should be used instead of the one returned by the server, if any.

send()

Sends the request.  If the request is asynchronous (which is the default), this method returns as soon as the request is sent.  If the request is synchronous, this method doesn't return until the response has arrived.

Note: Any event listeners you wish to set must be set before calling send().
void send(
  [optional] in nsIVariant body
);
Parameters
body
This may be an nsIDocument, nsIInputStream, or a string (an nsISupportsString if called from native code) that is used to populate the body of a POST request. Starting with Gecko 1.9.2, you may also specify an DOM  File , and starting with Gecko 1.9.3 you may also specify a FormData object.
Notes

If the body is an nsIDOMDocument, it is serialized before being sent.

If it's an nsIInputStream, it must be compatible with nsIUploadChannel's setUploadStream() method.  In that case, a Content-Length header is added to the request, with its value obtained using nsIInputStream's available() method.  Any headers included at the top of the stream are treated as part of the message body.  The stream's MIME type should be specified by setting the Content-Type header using the setRequestHeader() method prior to calling send().

Requires Gecko 1.9 (Firefox 3)

sendAsBinary()

A variant of the send() method that sends binary data.

void sendAsBinary(
  in DOMString body
);
Parameters
body
The request body as a DOM string.  This data is converted to a string of single-byte characters by truncation (removing the high-order byte of each character).

setRequestHeader()

Sets the value of an HTTP request header.

Note: You must call open() before using this method.
void setRequestHeader(
  in AUTF8String header,
  in AUTF8String value
);
Parameters
header
The name of the header whose value is to be set.
value
The value to set as the body of the header.

Implementation notes

XMLHttpRequest is implemented in Gecko using the nsIJSXMLHttpRequest and nsIXMLHttpRequest interfaces.

Page last modified 22:56, 4 May 2010 by TwelveBaud

Files (0)