XMLHttpRequest.open() 方法初始化一个请求。该方法要从JavaScript代码使用;从原生代码初始化一个请求,使用openRequest()
替代。
注意:为已激活的请求调用此方法(
open()
或openRequest()
已被调用)相当于调用abort()
。语法
xhrReq.open(method, url); xhrReq.open(method, url, async); xhrReq.open(method, url, async, user); xhrReq.open(method, url, async, user, password);
参数
method
- 要使用的HTTP方法,比如「GET」、「POST」、「PUT」、「DELETE」、等。对于非HTTP(S) URL被忽略。
url
- 一个
DOMString
表示要向其发送请求的URL。 async
可选- 一个可选的布尔参数,表示是否异步执行操作,默认为
true
。如果值为false
,send()
方法直到收到答复前不会返回。如果true
,已完成事务的通知可供事件监听器使用。如果multipart
属性为true
则这个必须为true
,否则将引发异常。注意:主线程上的同步请求很容易破坏用户体验,应该避免;实际上,许多浏览器已完全弃用主线程上的同步XHR支持。在Worker
中允许同步请求 user
可选- 可选的用户名用于认证用途;默认为
null
。 password
可选- 可选的密码用于认证用途,默认为
null
。
规格
Specification | Status | Comment |
---|---|---|
XMLHttpRequest open() |
Living Standard | WHATWG living standard |
浏览器兼容性
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | 1 | (Yes) | 5[1] 7 |
(Yes) | 1.2 |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | (Yes) | 1.0 | (Yes) | (Yes) | (Yes) | (Yes) |
[1] 该特性通过ActiveXObject()实现。Internet Explorer从7开始实现了标准的XMLHttpRequest。