XMLHttpRequest Interface
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.
Extends
Constructors
Code new XMLHttpRequest.get(String url, onSuccess(XMLHttpRequest request)) #
XMLHttpRequest.get(String url, onSuccess(XMLHttpRequest request));
Code new XMLHttpRequest() #
XMLHttpRequest();
Static Fields
Methods
Code String getAllResponseHeaders() #
string getAllResponseHeaders();
Returns all the response headers as a string, or null
if no response has been received. Note: For multipart requests, this returns the headers from the current part of the request, not from the original channel.
String getAllResponseHeaders();
Code String getResponseHeader(String header) #
null
if either the response has not yet been received or the header doesn't exist in the response.
String getResponseHeader(String header);
Code XMLHttpRequestEvents get on() #
XMLHttpRequestEvents get on();
Code void open(String method, String url, [bool async, String user, String password]) #
Initializes a request. This method is to be used from JavaScript code; to initialize a request from native code, use openRequest()
instead.
open()
or openRequest()
has already been called) is the equivalent of calling abort()
.Parameters
method
- The HTTP method to use; either "POST" or "GET". Ignored for non-HTTP(S) 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 isfalse
, thesend()
method does not return until the response is received. Iftrue
, notification of a completed transaction is provided using event listeners. This must be true if themultipart
attribute istrue
, 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.
void open(String method, String url, [bool async, String user, String password]);
Code void overrideMimeType(String override) #
send()
.
void overrideMimeType(String override);
Code void send([data]) #
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()
.Parameters
body
- This may be an
nsIDocument
,nsIInputStream
, or a string (annsISupportsString
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 aFormData
object.
void send([data]);
Code void $dom_addEventListener(String type, EventListener listener, [bool useCapture]) #
void $dom_addEventListener(String type, EventListener listener, [bool useCapture]);
Code void $dom_removeEventListener(String type, EventListener listener, [bool useCapture]) #
void $dom_removeEventListener(String type, EventListener listener, [bool useCapture]);
Fields
Code final int readyState #
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. |
final int readyState;
Code final Object response #
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.
final Object response;
Code final String responseText #
null
if the request was unsuccessful or has not yet been sent. Read-only.
final String responseText;
Code String responseType #
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 |
String responseType;
Code final Document responseXML #
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.final Document responseXML;
Code final int status #
status
is 200 for a successful request). Read-only.
final int status;
Code final String statusText #
status
, this includes the entire text of the response message ("200 OK
", for example). Read-only.
final String statusText;
Code final XMLHttpRequestUpload upload #
upload
.
New in Firefox 3.5
final XMLHttpRequestUpload upload;
Code bool withCredentials #
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
The default is false
.
bool withCredentials;
This page includes content from the Mozilla Foundation that is graciously licensed under a Creative Commons: Attribution-Sharealike license. Mozilla has no other association with Dart or dartlang.org. We encourage you to improve the web by contributing to The Mozilla Developer Network.