XMLHttpRequest (XHR) 对象用于与服务器交互。可以从 URL 检索数据,无需刷新整个页面。这使 Web 页面能够仅仅更新页面部分,不破坏用户正在做的事情。 XMLHttpRequest 大量用于 AJAX 编程。

  <div id="interfaceDiagram" style="display: inline-block; position: relative; width: 100%; padding-bottom: 23.076923076923077%; vertical-align: middle; overflow: hidden;"><svg style="display: inline-block; position: absolute; top: 0; left: 0;" viewbox="-50 0 650 150" preserveAspectRatio="xMinYMin meet"><a xlink:href="../API/EventTarget.html" target="_top"><rect x="1" y="1" width="110" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text  x="56" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">EventTarget</text></a><polyline points="111,25  121,20  121,30  111,25" stroke="#D4DDE4" fill="none"/><line x1="121" y1="25" x2="151" y2="25" stroke="#D4DDE4"/><a xlink:href="../API/XMLHttpRequestEventTarget" target="_top"><rect x="151" y="1" width="250" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text  x="276" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">XMLHttpRequestEventTarget</text></a><polyline points="401,25  411,20  411,30  401,25" stroke="#D4DDE4" fill="none"/><line x1="411" y1="25" x2="441" y2="25" stroke="#D4DDE4"/><a xlink:href="../API/XMLHttpRequest" target="_top"><rect x="441" y="1" width="140" height="50" fill="#F4F7F8" stroke="#D4DDE4" stroke-width="2px" /><text  x="511" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">XMLHttpRequest</text></a></svg></div>
					
  a:hover text { fill: #0095DD; pointer-events: all;}
					

不管它的名称, XMLHttpRequest 可以用于检索任何类型的数据,不仅仅是 XML。

若通信需要涉及从服务器接收事件数据 (或消息数据),考虑使用 服务器发送事件 透过 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.

特性

此接口还继承特性从 XMLHttpRequestEventTarget EventTarget .

XMLHttpRequest.onreadystatechange
EventHandler that is called whenever the readyState attribute changes.
XMLHttpRequest.readyState 只读
返回 unsigned short ,请求的状态。
XMLHttpRequest.response 只读
返回 ArrayBuffer , Blob , Document ,JavaScript 对象,或 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 只读
返回 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
unsigned long representing the number of milliseconds a request can take before automatically being terminated.
XMLHttpRequestEventTarget.ontimeout
EventHandler that is called whenever the request times out.
XMLHttpRequest.upload 只读
XMLHttpRequestUpload ,表示上传进程。
XMLHttpRequest.withCredentials
布尔 that indicates whether or not cross-site Access-Control requests should be made using credentials such as cookies or authorization headers.

非标特性

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 从 Gecko 22 起已过时
This Gecko-only feature, a boolean, was removed in Firefox/Gecko 22. 请使用 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 ,等)。见 使用 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()

中止请求若请求已发送。

XMLHttpRequest.getAllResponseHeaders()
返回所有响应头,分隔通过 CRLF ,以字符串形式,或 null 若未收到响应。
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()

初始化请求。

XMLHttpRequest.overrideMimeType()

覆写由服务器返回的 MIME 类型。

XMLHttpRequest.send()

发送请求。若请求为异步 (默认),此方法尽快返回在发送请求后。

XMLHttpRequest.setRequestHeader()
Sets the value of an HTTP request header. You must call setRequestHeader() after open() ,但先于 send() .

非标方法

XMLHttpRequest.init()
从 C++ 代码初始化要使用的对象。 警告: 此方法必须 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()
变体的 send() 方法,用于发送二进制数据。

事件

abort
Fired when a request has been aborted, for example because the program called XMLHttpRequest.abort() .
也可用凭借 onabort 特性。
error
被激发当请求遭遇错误。
也可用凭借 onerror 特性。
load
被激发当 XMLHttpRequest 事务成功完成。
也可用凭借 onload 特性。
loadend
Fired when a request has completed, whether successfully (after load ) or unsuccessfully (after abort or error ).
也可用凭借 onloadend 特性。
loadstart
Fired when a request has started to load data.
也可用凭借 onloadstart 特性。
progress
Fired periodically when a request receives more data.
也可用凭借 onprogress 特性。
timeout
Fired when progress is terminated due to preset time expiring.
也可用凭借 ontimeout 特性。

规范

规范 状态 注释
XMLHttpRequest 实时标准 实时标准,最新版本

浏览器兼容性

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
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 被设为 true ,默认为 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 被设为 true ,默认为 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 被设为 true ,默认为 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 被设为 true ,默认为 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 注意事项
polyfill available to support sendAsBinary() .
Edge 不支持 No 注意事项
No 注意事项
polyfill available to support sendAsBinary() .
Firefox 不支持 2 — 31 IE 不支持 No Opera 不支持 No Safari 不支持 No WebView Android 不支持 No 注意事项
No 注意事项
polyfill available to support sendAsBinary() .
Chrome Android 不支持 No 注意事项
No 注意事项
polyfill available to support sendAsBinary() .
Firefox Android 不支持 4 — 31 Opera Android 不支持 No Safari iOS 不支持 No Samsung Internet Android 不支持 No 注意事项
No 注意事项
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

图例

完整支持

完整支持

不支持

不支持

兼容性未知 ?

兼容性未知

非标。预期跨浏览器支持较差。

弃用。不要用于新网站。

弃用。不要用于新网站。

见实现注意事项。

另请参阅

元数据

  • 最后修改:
  1. 指南
    1. 使用 XMLHttpRequest
    2. HTML 在 XMLHttpRequest
    3. 使用 XMLHttpRequest 在 IE6
    4. XMLHttpRequest changes for Gecko1.8
  2. 接口
    1. FormData
    2. XMLHttpRequest
    3. XMLHttpRequestEventTarget
    4. XMLHttpRequestUpload

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1