XMLHttpRequest 构造函数,用于创建一个XMLHttpRequest实例
new XMLHttpRequest()
Example method:
If the request has been sent, the request is immediately aborted.
XMLHttpRequest.getAllResponseHeaders()
Return all response headers separated by CRLF in the form of character strings, or return if no response is received null
.
XMLHttpRequest.getResponseHeader()
Returns a string containing the specified response header. If the response has not been received or the header does not exist in the response, it is returned null
.
Initialize a request. This method can only be used in JavaScript code, to initialize the request in native code, please use openRequest()
.
XMLHttpRequest.overrideMimeType()
Rewrite the MIME type returned by the server.
send request. If the request is asynchronous (default), the method will return immediately after the request is sent.
XMLHttpRequest.setRequestHeader()
Set the value of the HTTP request header. You must call the method open()
after and send()
before setRequestHeader()
.
Instance attributes:
XMLHttpRequest.onreadystatechange
Called when the readyState property changes EventHandler
.
XMLHttpRequest.readyState
Read only
Returns an unsigned short (unsigned short) number, representing the requested status code.
XMLHttpRequest.response
Read only
Returns a ArrayBuffer
, Blob
, Document
, or DOMString
, in particular depending on which type XMLHttpRequest.responseType
of value. It contains the entire response body.
XMLHttpRequest.responseText
Read only
Returns one DOMString
, which DOMString
contains the response to the request, and returns if the request was not successful or has not been sent null
.
An enumerated value used to define the response type.
XMLHttpRequest.responseURL
Read only
Returns the serialized URL of the response. If the URL is empty, an empty string is returned.
XMLHttpRequest.responseXML
Read only
Returns one Document
, which contains the response to the request. If the request was unsuccessful, has not been sent, or cannot be parsed as XML or HTML, it is returned null
.
XMLHttpRequest.status
Read only
Return an unsigned short (unsigned short) number, representing the response status of the request.
XMLHttpRequest.statusText
Read only
Returns one DOMString
, which contains the response status returned by the HTTP server. The XMLHTTPRequest.status
difference is that it contains the complete response status text (for example, " 200 OK
").
Note: According to the HTTP / 2 specification ( 8.1.2.4 Response Pseudo-Header Fields ), HTTP / 2 does not define any method for carrying the version or reason phrase contained in the HTTP / 1.1 status line.
An unsigned long integer (unsigned long) number, indicating the maximum request time (milliseconds) of the request, if the time is exceeded, the request will automatically end.
XMLHttpRequestEventTarget.ontimeout
Called when the request times out EventHandler
.
XMLHttpRequest.upload
Read only
XMLHttpRequestUpload
, On behalf of the upload process.
XMLHttpRequest.withCredentials
One 布尔值
, used to specify whether cross-domain Access-Control
requests should carry authorization information, such as cookies or authorization headers.
example:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if (xhr.readyState == 4){
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304){
alert(xhr.responseText);
} else {
alert("Request was unsuccessful: " + xhr.status);
}
}
};
xhr.open("get", "example.txt", true);
xhr.send(null);
to sum up:
Steps for usage
1. Create an instance new new XMLHttpRequest ();
2. Add monitoring for onreadystatechange event
3.调用 xhr..open(method, url, async);
4. Set the request header xhr.setRequestHeader (header, value);
5. Send the request, xhr.send ()
xhr.send(null); // xhr.send('string');
//xhr.send(new Blob()); // xhr.send(new Int8Array()); // xhr.send({ form: 'data' }); // xhr.send(document);
xhr.send("foo=bar&lorem=ipsum"); // xhr.send('string');
//xhr.send(new Blob()); // xhr.send(new Int8Array()); // xhr.send({ form: 'data' }); // xhr.send(document);
6. The response is processed in onreadystatechange, or call xhr.abort () to interrupt the request