XMLHttpRequest

XMLHttpRequest (XHR) objects are used to interact with servers. You can retrieve data from a URL without having to do a full page refresh. This enables a Web page to update just part of a page without disrupting what the user is doing. XMLHttpRequest is used heavily in AJAX programming.

Despite its name, XMLHttpRequest can be used to retrieve any type of data, not just XML.

If your communication needs to involve receiving event data or message data from a server, consider using server-sent events through the EventSource interface. For full-duplex communication, WebSockets may be a better choice.

构造函数

XMLHttpRequest()
The constructor initializes an XMLHttpRequest. It must be called before any other method calls.

特性

This interface also inherits properties of XMLHttpRequestEventTarget and of EventTarget .

XMLHttpRequest.onreadystatechange
An EventHandler that is called whenever the readyState attribute changes.
XMLHttpRequest.readyState 只读
Returns an unsigned short , the state of the request.
XMLHttpRequest.response 只读
Returns an ArrayBuffer , Blob , Document , JavaScript object, or a DOMString , depending on the value of XMLHttpRequest.responseType , that contains the response entity body.
XMLHttpRequest.responseText 只读
返回 DOMString that contains the response to the request as text, or null if the request was unsuccessful or has not yet been sent.
XMLHttpRequest.responseType
Is an enumerated value that defines the response type.
XMLHttpRequest.responseURL 只读
Returns the serialized URL of the response or the empty string if the URL is null.
XMLHttpRequest.responseXML 只读
返回 Document containing the response to the request, or null if the request was unsuccessful, has not yet been sent, or cannot be parsed as XML or HTML. Not available in workers.
XMLHttpRequest.status 只读
Returns an unsigned short with the status of the response of the request.
XMLHttpRequest.statusText 只读
返回 DOMString containing the response string returned by the HTTP server. Unlike XMLHttpRequest.status , this includes the entire text of the response message (" 200 OK ", for example).

注意: According to the HTTP/2 specification ( 8.1.2.4 Response Pseudo-Header Fields ), HTTP/2 does not define a way to carry the version or reason phrase that is included in an HTTP/1.1 status line.

XMLHttpRequest.timeout
Is an unsigned long representing the number of milliseconds a request can take before automatically being terminated.
XMLHttpRequestEventTarget.ontimeout
Is an EventHandler that is called whenever the request times out.
XMLHttpRequest.upload 只读
Is an XMLHttpRequestUpload , representing the upload process.
XMLHttpRequest.withCredentials
Boolean that indicates whether or not cross-site Access-Control requests should be made using credentials such as cookies or authorization headers.

Non-standard properties

XMLHttpRequest.channel 只读
nsIChannel . The channel used by the object when performing the request.
XMLHttpRequest.mozAnon 只读
Is a boolean. If true, the request will be sent without cookie and authentication headers.
XMLHttpRequest.mozSystem 只读
Is a boolean. If true, the same origin policy will not be enforced on the request.
XMLHttpRequest.mozBackgroundRequest
Is a boolean. It indicates whether or not the object represents a background service request.
XMLHttpRequest.mozResponseArrayBuffer Obsolete since Gecko 6 只读
ArrayBuffer . The response to the request, as a JavaScript typed array.
XMLHttpRequest.multipart Obsolete since Gecko 22
This Gecko-only feature, a boolean, was removed in Firefox/Gecko 22. Please use Server-Sent Events , Web Sockets ,或 responseText from progress events instead.

事件处理程序

onreadystatechange as a property of the XMLHttpRequest instance is supported in all browsers.

Since then, a number of additional event handlers have been implemented in various browsers ( onload , onerror , onprogress , etc.). See Using XMLHttpRequest .

More recent browsers, including Firefox, also support listening to the XMLHttpRequest events via standard addEventListener() APIs in addition to setting on* properties to a handler function.

方法

XMLHttpRequest.abort()
Aborts the request if it has already been sent.
XMLHttpRequest.getAllResponseHeaders()
Returns all the response headers, separated by CRLF , as a string, or null if no response has been received.
XMLHttpRequest.getResponseHeader()
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.
XMLHttpRequest.open()
Initializes a request.
XMLHttpRequest.overrideMimeType()
Overrides the MIME type returned by the server.
XMLHttpRequest.send()
Sends the request. If the request is asynchronous (which is the default), this method returns as soon as the request is sent.
XMLHttpRequest.setRequestHeader()
Sets the value of an HTTP request header. You must call setRequestHeader() after open() , but before send() .

Non-standard methods

XMLHttpRequest.init()
Initializes the object for use from C++ code.
警告: This method must not be called from JavaScript.
XMLHttpRequest.openRequest()
Initializes a request. This method is to be used from native code; to initialize a request from JavaScript code, use open() instead. See the documentation for open() .
XMLHttpRequest.sendAsBinary()
A variant of the send() method that sends binary data.

Events

abort
Fired when a request has been aborted, for example because the program called XMLHttpRequest.abort() .
Also available via the onabort 特性。
error
Fired when the request encountered an error.
Also available via the onerror 特性。
load
Fired when an XMLHttpRequest transaction completes successfully.
Also available via the onload 特性。
loadend
Fired when a request has completed, whether successfully (after load ) or unsuccessfully (after abort or error ).
Also available via the onloadend 特性。
loadstart
Fired when a request has started to load data.
Also available via the onloadstart 特性。
progress
Fired periodically when a request receives more data.
Also available via the onprogress 特性。
timeout
Fired when progress is terminated due to preset time expiring.
Also available via the ontimeout 特性。

规范

规范 状态 Comment
XMLHttpRequest 实时标准 Live standard, latest version

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
XMLHttpRequest Chrome 1 Edge 12
12
12 — 79
注意事项
Implemented via ActiveXObject('Microsoft.XMLHTTP')
Firefox 1 IE 7
7
5
注意事项
Implemented via ActiveXObject('Microsoft.XMLHTTP')
Opera 8 Safari 1.2 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0
abort Chrome 18 Edge 12 Firefox Yes IE 5 Opera Yes Safari 1.2 WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
abort event Chrome Yes Edge ≤79 Firefox Yes IE 10 Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS ? Samsung Internet Android Yes
error event Chrome Yes Edge ≤79 Firefox Yes IE 10 Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS ? Samsung Internet Android Yes
getAllResponseHeaders Chrome 1 Edge 12 Firefox 4
注意事项
4
注意事项
Starting from Firefox 49, empty headers are returned as empty strings in case the preference network.http.keep_empty_response_headers_as_empty_string is set to true , defaulting to false . Before Firefox 49 empty headers had been ignored. Since Firefox 50 the preference defaults to true .
IE 5 Opera Yes Safari 1.2 WebView Android Yes Chrome Android Yes Firefox Android 4
注意事项
4
注意事项
Starting from Firefox 49, empty headers are returned as empty strings in case the preference network.http.keep_empty_response_headers_as_empty_string is set to true , defaulting to false . Before Firefox 49 empty headers had been ignored. Since Firefox 50 the preference defaults to true .
Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
getResponseHeader Chrome 1 Edge 12 Firefox 1
注意事项
1
注意事项
Starting from Firefox 49, empty headers are returned as empty strings in case the preference network.http.keep_empty_response_headers_as_empty_string is set to true , defaulting to false . Before Firefox 49 empty headers had been ignored. Since Firefox 50 the preference defaults to true .
IE 5 Opera 8 Safari 1.2 WebView Android 1 Chrome Android 18 Firefox Android 4
注意事项
4
注意事项
Starting from Firefox 49, empty headers are returned as empty strings in case the preference network.http.keep_empty_response_headers_as_empty_string is set to true , defaulting to false . Before Firefox 49 empty headers had been ignored. Since Firefox 50 the preference defaults to true .
Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0
load event Chrome Yes Edge ≤79 Firefox Yes IE 9 Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS ? Samsung Internet Android Yes
loadend event Chrome Yes Edge ≤79 Firefox Yes IE 10 Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS ? Samsung Internet Android Yes
loadstart event Chrome Yes Edge ≤79 Firefox Yes IE 10 Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS ? Samsung Internet Android Yes
onreadystatechange Chrome 1 Edge 12 Firefox 1 IE 5 Opera 9 Safari 1.2 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0
open Chrome 1 Edge 12 Firefox 1
注意事项
1
注意事项
Starting in Firefox 30, synchronous requests on the main thread have been deprecated due to their negative impact on performance and the user experience. Therefore, the async parameter may not be false except in a Worker .
IE 5 Opera 8 Safari 1.2 WebView Android 1 Chrome Android 18 Firefox Android 4
注意事项
4
注意事项
Starting in Firefox 30, synchronous requests on the main thread have been deprecated due to their negative impact on performance and the user experience. Therefore, the async parameter may not be false except in a Worker .
Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0
overrideMimeType Chrome 1 Edge 12 Firefox Yes IE 11
11
5
注意事项
Implemented via ActiveXObject
Opera Yes Safari 1.2 WebView Android Yes Chrome Android 18 Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android 1.0
progress event Chrome Yes Edge ≤79 Firefox Yes IE 10 Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS ? Samsung Internet Android Yes
readyState Chrome 1 Edge 12 Firefox 1 IE 7 Opera 8 Safari 1.2 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0
response Chrome 9 Edge 12 Firefox 6 IE 10 Opera 11.6 Safari 5.1 WebView Android ≤37 Chrome Android 18 Firefox Android 6 Opera Android 12 Safari iOS 6 Samsung Internet Android 1.0
responseText Chrome 1 Edge 12 Firefox 1 IE 5
注意事项
5
注意事项
Before Internet Explorer 10, the value of XMLHttpRequest.responseText could be read only once the request was complete.
Opera 8 Safari 1.2 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0
responseType Chrome 31 Edge 12 Firefox 6 IE 10 Opera 18 Safari 7 WebView Android 55 Chrome Android 55 Firefox Android 50 Opera Android 42 Safari iOS 7 Samsung Internet Android 6.0
responseURL Chrome 37 Edge 14 Firefox 32 IE 不支持 No Opera 24 Safari 8 WebView Android 37 Chrome Android 37 Firefox Android 32 Opera Android 24 Safari iOS Yes Samsung Internet Android 3.0
responseXML Chrome Yes Edge 12 Firefox Yes
注意事项
Yes
注意事项
Prior to Firefox 51, an error parsing the received data added a <parsererror> node to the top of the Document and then returned the Document in whatever state it happens to be in. This was inconsistent with the specification. Starting with Firefox 51, this scenario now correctly returns null as per the spec.
IE Yes Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes
注意事项
Yes
注意事项
Prior to Firefox 51, an error parsing the received data added a <parsererror> node to the top of the Document and then returned the Document in whatever state it happens to be in. This was inconsistent with the specification. Starting with Firefox 51, this scenario now correctly returns null as per the spec.
Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
send Chrome 1 Edge 12 Firefox 1 IE 5 Opera 8 Safari 1.2 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0
sendAsBinary
弃用 非标
Chrome 不支持 No
注意事项
No
注意事项
There is a polyfill available to support sendAsBinary() .
Edge 不支持 No
注意事项
No
注意事项
There is a polyfill available to support sendAsBinary() .
Firefox 不支持 2 — 31 IE 不支持 No Opera 不支持 No Safari 不支持 No WebView Android 不支持 No
注意事项
No
注意事项
There is a polyfill available to support sendAsBinary() .
Chrome Android 不支持 No
注意事项
No
注意事项
There is a polyfill available to support sendAsBinary() .
Firefox Android 不支持 4 — 31 Opera Android 不支持 No Safari iOS 不支持 No Samsung Internet Android 不支持 No
注意事项
No
注意事项
There is a polyfill available to support sendAsBinary() .
setRequestHeader Chrome 1 Edge 12 Firefox 1 IE 5 Opera 8 Safari 1.2 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0
status Chrome 1 Edge 12 Firefox 1 IE 7
注意事项
7
注意事项
Internet Explorer version 5 and 6 supported ajax calls using ActiveXObject()
Opera 8 Safari 1.2 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0
statusText Chrome 1 Edge 12 Firefox 1 IE 7
注意事项
7
注意事项
Internet Explorer version 5 and 6 supported ajax calls using ActiveXObject()
Opera Yes Safari 1.2 WebView Android Yes Chrome Android 18 Firefox Android 4 Opera Android Yes Safari iOS Yes Samsung Internet Android 1.0
timeout Chrome 29 Edge 12 Firefox 12 IE 8 Opera 17
17
12 — 16
Safari 6.1 WebView Android ≤37 Chrome Android 29 Firefox Android 14 Opera Android 18
18
12 — 16
Safari iOS 7 Samsung Internet Android 2.0
timeout event Chrome Yes Edge ≤18 Firefox Yes IE 10 Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
upload Chrome 1 Edge 12 Firefox Yes IE 10 Opera Yes Safari 10 WebView Android Yes Chrome Android 18 Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android 1.0
withCredentials Chrome 3 Edge 12 Firefox 3.5
注意事项
3.5
注意事项
Starting with Firefox 11, it's no longer supported to use the withCredentials attribute when performing synchronous requests. Attempting to do so throws an NS_ERROR_DOM_INVALID_ACCESS_ERR 异常。
IE 10
注意事项
10
注意事项
Internet Explorer versions 8 and 9 supported cross-domain requests (CORS) using XDomainRequest .
Opera 12 Safari 4 WebView Android ≤37 Chrome Android 18 Firefox Android 4
注意事项
4
注意事项
Starting with Firefox 11, it's no longer supported to use the withCredentials attribute when performing synchronous requests. Attempting to do so throws an NS_ERROR_DOM_INVALID_ACCESS_ERR 异常。
Opera Android 12 Safari iOS 3.2 Samsung Internet Android 1.0

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
弃用。不要用于新网站。
见实现注意事项。

另请参阅