# RequestTask wx.request(Object object)
以 Promise 风格 调用:不支持
微信 Windows 版:支持
微信 Mac 版:支持
相关文档: 网络使用说明、局域网通信、移动解析HttpDNS
# 功能描述
发起 HTTPS 网络请求。使用前请注意阅读相关说明。
# 参数
# Object object
属性 | 类型 | 默认值 | 必填 | 说明 | 最低版本 | |||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
url | string | 是 | 开发者服务器接口地址 | |||||||||||||||||||||
data | string/object/ArrayBuffer | 否 | 请求的参数 | |||||||||||||||||||||
header | Object | 否 | 设置请求的 header,header 中不能设置 Referer。content-type 默认为 application/json | |||||||||||||||||||||
timeout | number | 否 | 超时时间,单位为毫秒。默认值为 60000 | 2.10.0 | ||||||||||||||||||||
method | string | GET | 否 | HTTP 请求方法 | ||||||||||||||||||||
| ||||||||||||||||||||||||
dataType | string | json | 否 | 返回的数据格式 | ||||||||||||||||||||
| ||||||||||||||||||||||||
responseType | string | text | 否 | 响应的数据类型 | 1.7.0 | |||||||||||||||||||
| ||||||||||||||||||||||||
enableHttp2 | boolean | false | 否 | 开启 http2 | 2.10.4 | |||||||||||||||||||
enableQuic | boolean | false | 否 | 开启 quic | 2.10.4 | |||||||||||||||||||
enableCache | boolean | false | 否 | 开启 cache | 2.10.4 | |||||||||||||||||||
enableHttpDNS | boolean | false | 否 | 是否开启 HttpDNS 服务。如开启,需要同时填入 httpDNSServiceId 。 HttpDNS 用法详见 移动解析HttpDNS | 2.19.1 | |||||||||||||||||||
httpDNSServiceId | string | 否 | HttpDNS 服务商 Id。 HttpDNS 用法详见 移动解析HttpDNS | 2.19.1 | ||||||||||||||||||||
enableChunked | boolean | false | 否 | 开启 transfer-encoding chunked。 | 2.20.2 | |||||||||||||||||||
forceCellularNetwork | boolean | false | 否 | wifi下使用移动网络发送请求 | 2.21.0 | |||||||||||||||||||
success | function | 否 | 接口调用成功的回调函数 | |||||||||||||||||||||
fail | function | 否 | 接口调用失败的回调函数 | |||||||||||||||||||||
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
# object.success 回调函数
# 参数
# Object res
属性 | 类型 | 说明 | 最低版本 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
data | string/Object/Arraybuffer | 开发者服务器返回的数据 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
statusCode | number | 开发者服务器返回的 HTTP 状态码 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
header | Object | 开发者服务器返回的 HTTP Response Header | 1.2.0 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
cookies | Array.<string> | 开发者服务器返回的 cookies,格式为字符串数组 | 2.10.0 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
profile | Object | 网络请求过程中一些调试信息,查看详细说明 | 2.10.4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
# object.fail 回调函数
# 参数
# Object err
# 返回值
# RequestTask
基础库 1.4.0 开始支持,低版本需做兼容处理。
请求任务对象
# data 参数说明
最终发送给服务器的数据是 String 类型,如果传入的 data 不是 String 类型,会被转换成 String 。转换规则如下:
- 对于
GET
方法的数据,会将数据转换成 query string(encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...
) - 对于
POST
方法且header['content-type']
为application/json
的数据,会对数据进行 JSON 序列化 - 对于
POST
方法且header['content-type']
为application/x-www-form-urlencoded
的数据,会将数据转换成 query string(encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...)
# 示例代码
wx.request({
url: 'example.php', //仅为示例,并非真实的接口地址
data: {
x: '',
y: ''
},
header: {
'content-type': 'application/json' // 默认值
},
success (res) {
console.log(res.data)
}
})