使用XMLHttpRequest (XHR)对象可以与服务器交互。您可以从URL获取数据,而无需让整个的页面刷新。这使得Web页面可以只更新页面的局部,而不影响用户的操作。XMLHttpRequest在 Ajax 编程中被大量使用。

尽管名称如此,XMLHttpRequest可以用于获取任何类型的数据,而不仅仅是XML,它还支持   HTTP以外的协议(包括文件和ftp)。

如果您的通信需要从服务器接收事件或消息数据,请考虑通过EventSource接口使用 server-sent events。对于 full-duplex 通信, WebSockets 可能是更好的选择。

构造方法

XMLHttpRequest()
构造函数初始化一个 XMLHttpRequest 对象。必须在所有其他方法被调用前调用构造函数。

属性

此接口继承了 XMLHttpRequestEventTargetEventTarget 的属性。

XMLHttpRequest.onreadystatechange

当readyState属性发生变化时调用的EventHandler

XMLHttpRequest.readyState 只读

返回 一个unsigned short 即无符号短整型,请求的状态码。

XMLHttpRequest.response 只读

返回ArrayBufferBlobDocumentDOMString},具体是哪种类型取决于XMLHttpRequest.responseType的值。其中包含响应体body。

XMLHttpRequest.responseText 只读

返回一个DOMString},该DOMString}包含对请求的响应,如果请求未成功或尚未发送,则返回null。

XMLHttpRequest.responseType

定义响应类型的枚举值。

XMLHttpRequest.responseURL 只读

返回响应的序列化URL,如果URL为空,则返回空字符串。

XMLHttpRequest.responseXML 只读 Not available to workers

返回一个Document,其中包含该请求的响应,如果请求未成功、尚未发送或不能解析为XML或HTML,则返回null。

XMLHttpRequest.status 只读

返回  unsigned short 即无符号短整型请求响应状态。

XMLHttpRequest.statusText 只读

返回一个DOMString},其中包含HTTP服务器返回的响应状态。与 XMLHTTPRequest.status不同的是,它包括响应状态的整个文本(例如,“200 OK”)。

注意:根据HTTP/2规范(8.1.2.4响应伪标头字段),HTTP/2没有定义一种方式来携带HTTP/1.1状态行中包含的版本或原因短语。

XMLHttpRequest.timeout

unsigned long 即无符号长整型,表示该请求的最大请求时间(毫秒),超过该时间请求会自动结束。

XMLHttpRequestEventTarget.ontimeout

当请求超时调用的EventHandler。{ { gecko_minversion_inline(" 12.0 ")} }

XMLHttpRequest.upload 只读

XMLHttpRequestUpload,表示上传过程。

XMLHttpRequest.withCredentials

Boolean,用来指定跨域的请求是否应该使用证书(如cookie或授权header头)。

XMLHttpRequest.channel只读

nsIChannel,对象在执行请求时使用的通道。

XMLHttpRequest.mozAnon只读

一个布尔值,如果为真,请求将在没有cookie和身份验证header头的情况下发送。

XMLHttpRequest.mozSystem只读

一个布尔值,如果为真,则在请求时不会强制执行同源策略。

XMLHttpRequest.mozBackgroundRequest

一个布尔值,它指示对象是否是后台服务器端的请求

XMLHttpRequest.mozResponseArrayBuffer 已废弃 Gecko 6 只读

一个ArrayBuffer类型,把请求的响应作为一个TypedArrays。

XMLHttpRequest.multipart已废弃 Gecko 22

这个Gecko的独有属性,是一个布尔值,在Firefox/Gecko 22中被删除了。请使用Server-Sent EventsWeb Sockets, 或来自进度事件的responseText代替。

方法

XMLHttpRequest.abort()

如果请求已经被发送,则立刻中止请求.

XMLHttpRequest.getAllResponseHeaders()

以字符串的形式返回所有用CRLF分隔的响应头,如果没有收到响应,则返回null。

XMLHttpRequest.getResponseHeader()

返回包含指定响应头的字符串,如果响应尚未收到或响应中不存在该报头,则返回null。

XMLHttpRequest.open()

初始化一个请求。该方法只能JavaScript代码中使用,若要在native code中初始化请求,请使用openRequest()。

XMLHttpRequest.overrideMimeType()

重写由服务器返回的MIME type。

XMLHttpRequest.send()

发送请求。如果请求是异步的(默认),那么该方法将在请求发送后立即返回。

XMLHttpRequest.setRequestHeader()

设置HTTP请求头的值。您必须在open()之后、send()之前调用setRequestHeader()这个方法。

非标准方法

init()

在 C++代码中初始化一个XHR对象.

警告: 该方法不能在 JavaScript 代码中使用.

openRequest()

初始化一个请求. 这个方法用于本地代码; 如果用JavaScript 代码来初始化请求, 使用 open()代替. 看文档open().

XMLHttpRequest.sendAsBinary()

send()方法的变体,用来发送二进制数据。

规范

规范     状态     注释
XMLHttpRequest Living Standard     Live standard, latest version


 

 

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic supportChrome Full support 1Edge Full support YesFirefox Full support 1IE Full support 7Opera Full support YesSafari Full support 1.2WebView Android Full support YesChrome Android Full support 18Edge Mobile Full support 12Firefox Android Full support 4Opera Android Full support YesSafari iOS ? Samsung Internet Android Full support Yes
onreadystatechangeChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 7
Notes
Full support 7
Notes
Notes Internet Explorer version 5 and 6 supported ajax calls using ActiveXObject()
Opera Full support YesSafari Full support 1.2WebView Android Full support 1Chrome Android Full support 18Edge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS ? Samsung Internet Android Full support Yes
readyStateChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 7Opera Full support YesSafari Full support 1.2WebView Android Full support YesChrome Android Full support 18Edge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS ? Samsung Internet Android Full support Yes
responseChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS ? Samsung Internet Android Full support Yes
responseTextChrome Full support YesEdge Full support 12Firefox Full support YesIE ?
Notes
?
Notes
Notes Before IE 10, the value of XMLHttpRequest.responseText could be read only once the request was complete.
Opera Full support YesSafari Full support 10WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS ? Samsung Internet Android Full support Yes
responseTypeChrome Full support 31Edge Full support 12Firefox Full support 6IE Full support 10Opera Full support 18Safari Full support 7WebView Android Full support 55Chrome Android Full support 55Edge Mobile Full support YesFirefox Android Full support 50Opera Android Full support YesSafari iOS ? Samsung Internet Android Full support 6.0
responseURLChrome Full support 37Edge Full support 14Firefox Full support 32IE No support NoOpera Full support 24Safari Full support 8WebView Android Full support 37Chrome Android Full support 37Edge Mobile ? Firefox Android Full support 32Opera Android Full support 24Safari iOS ? Samsung Internet Android Full support Yes
responseXMLChrome Full support YesEdge Full support 12Firefox Full support Yes
Notes
Full support Yes
Notes
Notes 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 Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support Yes
Notes
Full support Yes
Notes
Notes 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 Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
statusChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 7
Notes
Full support 7
Notes
Notes Internet Explorer version 5 and 6 supported ajax calls using ActiveXObject()
Opera Full support YesSafari Full support 1.2WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS ? Samsung Internet Android Full support Yes
statusTextChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 7
Notes
Full support 7
Notes
Notes Internet Explorer version 5 and 6 supported ajax calls using ActiveXObject()
Opera Full support YesSafari Full support 1.2WebView Android Full support YesChrome Android Full support 18Edge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS ? Samsung Internet Android Full support Yes
timeoutChrome Full support 29Edge Full support 12Firefox Full support 12IE Full support 8Opera Full support 17
Full support 17
No support 12 — 16
Safari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS ? Samsung Internet Android Full support Yes
uploadChrome Full support 1Edge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari Full support 10WebView Android Full support YesChrome Android Full support 18Edge Mobile ? Firefox Android ? Opera Android Full support YesSafari iOS ? Samsung Internet Android Full support Yes
withCredentialsChrome Full support YesEdge Full support 12Firefox Full support 3.5
Notes
Full support 3.5
Notes
Notes 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 exception.
IE Full support 10
Notes
Full support 10
Notes
Notes Internet Explorer versions 8 and 9 supported cross-domain requests (CORS) using XDomainRequest
Opera Full support 12Safari Full support 4WebView Android Full support YesChrome Android Full support YesEdge Mobile ? Firefox Android Full support 4
Notes
Full support 4
Notes
Notes 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 exception.
Opera Android Full support YesSafari iOS ? Samsung Internet Android Full support Yes
abortChrome Full support 18Edge Full support 12Firefox Full support YesIE Full support 7
Full support 7
Full support 5
Notes
Notes Implemented via ActiveXObject
Opera Full support YesSafari Full support 1.2WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS ? Samsung Internet Android Full support Yes
getAllResponseHeadersChrome Full support 1Edge Full support 12Firefox Full support 4
Notes
Full support 4
Notes
Notes 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 Full support 7
Full support 7
Full support 5
Notes
Notes Implemented via ActiveXObject
Opera Full support YesSafari Full support 1.2WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4
Notes
Full support 4
Notes
Notes 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 Full support YesSafari iOS ? Samsung Internet Android Full support Yes
getResponseHeaderChrome Full support 1Edge Full support 12Firefox Full support Yes
Notes
Full support Yes
Notes
Notes 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 Full support 7
Full support 7
Full support 5
Notes
Notes Implemented via ActiveXObject
Opera Full support YesSafari Full support 1.2WebView Android Full support YesChrome Android Full support 18Edge Mobile Full support YesFirefox Android Full support Yes
Notes
Full support Yes
Notes
Notes 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 Full support YesSafari iOS ? Samsung Internet Android Full support Yes
openChrome Full support 1Edge Full support 12Firefox Full support Yes
Notes
Full support Yes
Notes
Notes 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 Full support 7
Full support 7
Full support 5
Notes
Notes Implemented via ActiveXObject
Opera Full support YesSafari Full support 1.2WebView Android Full support YesChrome Android Full support 18Edge Mobile Full support YesFirefox Android Full support Yes
Notes
Full support Yes
Notes
Notes 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 Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
overrideMimeTypeChrome Full support 1Edge Full support 12Firefox Full support YesIE Full support 11
Full support 11
Full support 5
Notes
Notes Implemented via ActiveXObject
Opera Full support YesSafari Full support 1.2WebView Android Full support YesChrome Android Full support 18Edge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
sendChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 7
Full support 7
Full support 5
Notes
Notes Implemented via ActiveXObject
Opera Full support YesSafari Full support 1.2WebView Android Full support YesChrome Android Full support 18Edge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
sendAsBinary
DeprecatedNon-standard
Chrome No support No
Notes
No support No
Notes
Notes There is a polyfill available to support sendAsBinary().
Edge No support NoFirefox No support 2 — 31IE No support NoOpera No support NoSafari No support NoWebView Android No support No
Notes
No support No
Notes
Notes There is a polyfill available to support sendAsBinary().
Chrome Android No support No
Notes
No support No
Notes
Notes There is a polyfill available to support sendAsBinary().
Edge Mobile No support NoFirefox Android No support 4 — 31Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
setRequestHeaderChrome Full support 1Edge Full support 12Firefox Full support YesIE Full support 7
Full support 7
Full support 5
Notes
Notes Implemented via ActiveXObject
Opera Full support YesSafari Full support 1.2WebView Android Full support YesChrome Android Full support 18Edge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.
See implementation notes.
See implementation notes.

相关