public interface XMLHttpRequest extends EventTarget
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.
| Modifier and Type | Field and Description |
|---|---|
static int |
DONE
The operation is complete.
|
static int |
HEADERS_RECEIVED
send() has been called, and headers and status are available. |
static int |
LOADING
Downloading;
responseText holds partial data. |
static int |
OPENED
send()has not been called yet. |
static int |
UNSENT
open()has not been called yet. |
| Modifier and Type | Method and Description |
|---|---|
void |
abort()
Aborts the request if it has already been sent.
|
EventRemover |
addEventListener(String type,
EventListener listener)
Register an event handler of a specific event type on the
EventTarget. |
EventRemover |
addEventListener(String type,
EventListener listener,
boolean useCapture)
Register an event handler of a specific event type on the
EventTarget. |
boolean |
dispatchEvent(Event evt)
Dispatch an event to this
EventTarget. |
String |
getAllResponseHeaders()
string getAllResponseHeaders();
|
EventListener |
getOnabort() |
EventListener |
getOnerror() |
EventListener |
getOnload() |
EventListener |
getOnloadend() |
EventListener |
getOnloadstart() |
EventListener |
getOnprogress() |
EventListener |
getOnreadystatechange() |
int |
getReadyState()
The state of the request:
|
Object |
getResponse()
The response entity body according to
responseType, as an ArrayBuffer, Blob, Document
, JavaScript object (for "moz-json"), or string. |
Blob |
getResponseBlob() |
String |
getResponseHeader(String header)
Returns the 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. |
String |
getResponseText()
The response to the request as text, or
null if the request was unsuccessful or has not yet been sent. |
String |
getResponseType()
Can be set to change the response type.
|
Document |
getResponseXML()
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. |
int |
getStatus()
The status of the response to the request.
|
String |
getStatusText()
The response string returned by the HTTP server.
|
XMLHttpRequestUpload |
getUpload()
The upload process can be tracked by adding an event listener to
upload. |
boolean |
isAsBlob() |
boolean |
isWithCredentials()
Indicates whether or not cross-site Access-Control requests should be made using credentials such as cookies or authorization headers.
|
void |
open(String method,
String url)
Initializes a request.
|
void |
open(String method,
String url,
boolean async)
Initializes a request.
|
void |
open(String method,
String url,
boolean async,
String user)
Initializes a request.
|
void |
open(String method,
String url,
boolean async,
String user,
String password)
Initializes a request.
|
void |
overrideMimeType(String override)
Overrides the MIME type returned by the server.
|
void |
removeEventListener(String type,
EventListener listener)
Removes an event listener from the
EventTarget. |
void |
removeEventListener(String type,
EventListener listener,
boolean useCapture)
Removes an event listener from the
EventTarget. |
void |
send()
Sends the request.
|
void |
send(ArrayBuffer data)
Sends the request.
|
void |
send(Blob data)
Sends the request.
|
void |
send(Document data)
Sends the request.
|
void |
send(FormData data)
Sends the request.
|
void |
send(String data)
Sends the request.
|
void |
setAsBlob(boolean arg) |
void |
setOnabort(EventListener arg) |
void |
setOnerror(EventListener arg) |
void |
setOnload(EventListener arg) |
void |
setOnloadend(EventListener arg) |
void |
setOnloadstart(EventListener arg) |
void |
setOnprogress(EventListener arg) |
void |
setOnreadystatechange(EventListener arg) |
void |
setRequestHeader(String header,
String value)
Sets the value of an HTTP request header.You must call
open()before using this method. |
void |
setResponseType(String arg) |
void |
setWithCredentials(boolean arg) |
static final int DONE
static final int HEADERS_RECEIVED
send() has been called, and headers and status are available.static final int LOADING
responseText holds partial data.static final int OPENED
send()has not been called yet.static final int UNSENT
open()has not been called yet.boolean isAsBlob()
void setAsBlob(boolean arg)
EventListener getOnabort()
void setOnabort(EventListener arg)
EventListener getOnerror()
void setOnerror(EventListener arg)
EventListener getOnload()
void setOnload(EventListener arg)
EventListener getOnloadend()
void setOnloadend(EventListener arg)
EventListener getOnloadstart()
void setOnloadstart(EventListener arg)
EventListener getOnprogress()
void setOnprogress(EventListener arg)
EventListener getOnreadystatechange()
void setOnreadystatechange(EventListener arg)
int getReadyState()
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. |
Object getResponse()
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.Blob getResponseBlob()
String getResponseText()
null if the request was unsuccessful or has not yet been sent. Read-only.String getResponseType()
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 |
void setResponseType(String arg)
Document getResponseXML()
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.int getStatus()
status is 200 for a successful request). Read-only.String getStatusText()
status, this includes the entire text of the response message ("200 OK", for example). Read-only.XMLHttpRequestUpload getUpload()
upload.
New in Firefox 3.5boolean isWithCredentials()
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.
void setWithCredentials(boolean arg)
void abort()
EventRemover addEventListener(String type, EventListener listener)
EventTargetEventTarget.addEventListener in interface EventTargetEventRemover addEventListener(String type, EventListener listener, boolean useCapture)
EventTargetEventTarget.addEventListener in interface EventTargetboolean dispatchEvent(Event evt)
EventTargetEventTarget.dispatchEvent in interface EventTargetString 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 getResponseHeader(String header)
null if either the response has not yet been received or the header doesn't exist in the response.void open(String method, String url)
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().methodurlasynctrue, 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.userpasswordvoid open(String method, String url, boolean async)
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().methodurlasynctrue, 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.userpasswordvoid open(String method, String url, boolean async, String user)
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().methodurlasynctrue, 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.userpasswordvoid open(String method, String url, boolean 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().methodurlasynctrue, 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.userpasswordvoid overrideMimeType(String override)
send().void removeEventListener(String type, EventListener listener)
EventTargetEventTarget.removeEventListener in interface EventTargetvoid removeEventListener(String type, EventListener listener, boolean useCapture)
EventTargetEventTarget.removeEventListener in interface EventTargetvoid 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.
send().bodynsIDocument, 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.void send(ArrayBuffer 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().bodynsIDocument, 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.void send(Blob 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().bodynsIDocument, 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.void send(Document 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().bodynsIDocument, 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.void send(String 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().bodynsIDocument, 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.void send(FormData 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().bodynsIDocument, 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.Copyright © 2016. All rights reserved.