XMLHttpRequest.open() 方法创建一个请求。这个方法仅限于Javascript代码中,在native中创建一个请求,请使用openRequest()。
注意: 如果XMLHttpRequest对象创建请求之后,再次调用此方法,此时的作用就相当于调用
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。 async可选- 可选的布尔值,默认为
true。表示要不要用异步的方式执行当前操作。如果值为false。send()方法会等到数据返回之后才会响应。如果值为true,就会使用事件监听的方式反映事件已完成。如果multipart的值为true那这个值必须为true,否则就会报错。注意: 从Gecko 30.0 (Firefox 30.0 / Thunderbird 30.0 / SeaMonkey 2.27)开始,考虑到用户体验,同步请求已经被标记为废弃。 user可选- 可选的,用来认证的用户名。默认值为
null。 password可选- 可选的,用来认证的密码,默认值为
null。
说明
| Specification | Status | Comment |
|---|---|---|
| XMLHttpRequest open() |
Living Standard | WHATWG living standard |
兼容性
| 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。