XMLHttpRequest
is a JavaScript object that was designed by Microsoft and adopted by Mozilla, Apple, and Google. It's 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 protocols 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.
The state of the request:
Value | State | Description |
0 | UNSENT | open() has not been called yet. |
1 | OPENED | send() has not been called yet. |
2 | HEADERS_RECEIVED | send() has been called, and headers and status are available. |
3 | LOADING | Downloading; responseText holds partial data. |
4 | DONE | The operation is complete. |
responseType
, as an ArrayBuffer
, Blob
, Document
, JavaScript object (for "moz-json"), or string. This is NULL
if the request is not complete or was not successful. Getter throws DOMException.null
if the request was unsuccessful or has not yet been sent. Read-only. Getter throws DOMException.Can be set to change the response type. This tells the server what format you want the response to be in.
Value | Data type of response property |
empty string | String (this is the default) |
"arraybuffer" | ArrayBuffer |
"blob" | Blob
|
"document" | Document
|
"text" | String |
"moz-json" | JavaScript object, parsed from a JSON string returned by the server Requires Gecko 9.0 |
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.
text/xml
Content-Type header, you can use overrideMimeType()
to force XMLHttpRequest
to parse it as XML anyway.status
is 200 for a successful request). Read-only. Getter throws DOMException.status
, this includes the entire text of the response message ("200 OK
", for example). Read-only. Getter throws DOMException.upload
.
New in Firefox 3.5Indicates whether or not cross-site Access-Control requests should be made using credentials such as cookies or authorization headers. New in Firefox 3.5
The default is false
.
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.
send()
.body
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 DOMFile
, and starting with Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1)
you may also specify a FormData
object.send()
has been called, and headers and status are available.responseText
holds partial data.send()
has not been called yet.open()
has not been called yet.