以Promise风格调用: 支持
发起 HTTPS 网络请求
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
url | string | 是 | 开发者服务器接口地址(推荐使用 encodeURIComponent 对参数部分进行编码) | |
data | string,object,ArrayBuffer | 否 | 请求的参数 | |
header | object | 否 | 设置请求的 header,header 中不能设置 Referer。content-type 默认为 application/json | |
timeout | number | 否 | 超时时间,单位为毫秒 | |
method | string | 否 | HTTP 请求方法 | |
dataType | string | 否 | 返回的数据格式 | |
responseType | string | 否 | 响应的数据类型 | |
success | function | 否 | 接口调用成功的回调函数 | |
fail | function | 否 | 接口调用失败的回调函数 | |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
值 | 说明 |
---|---|
OPTIONS | HTTP 请求 OPTIONS |
GET | HTTP 请求 GET |
POST | HTTP 请求 POST |
PUT | HTTP 请求 PUT |
DELETE | HTTP 请求 DELETE |
值 | 说明 |
---|---|
json | 返回的数据为 JSON,返回后会对返回的数据进行一次 JSON.parse |
其他 | 不对返回的内容进行 JSON.parse |
值 | 说明 |
---|---|
text | 响应的数据为文本 |
arraybuffer | 响应的数据为 ArrayBuffer |
属性 | 类型 | 说明 |
---|---|---|
data | string,object,Arraybuffer | 开发者服务器返回的数据 |
statusCode | number | 开发者服务器返回的 HTTP 状态码 |
header | object | 开发者服务器返回的 HTTP Response Header |
cookies | array | 开发者服务器返回的 cookies,格式为字符串数组 |
最终发送给服务器的数据是 String 类型,如果传入的 data 不是 String 类型,会被转换成 String 。转换规则如下:
xhs.request({ url: 'example.php', //仅为示例,并非真实的接口地址 method: 'POST' data: { x: '', y: '' }, header: { 'content-type': 'application/json' // 默认值 }, success (res) { console.log(res.data) } })