一、不用授权获取用户信息——open-data
用于展示微信开放的数据。
属性 | 类型 | 默认值 | 必填 | 说明 | 最低版本 |
---|---|---|---|---|---|
type | string | 否 | 开放数据类型 | 1.4.0 | |
open-gid | string | 否 | 当 type="groupName" 时生效, 群id | 1.4.0 | |
lang | string | en | 否 | 当 type="user*" 时生效,以哪种语言展示 userInfo | 1.4.0 |
type 的合法值
值 | 说明 | 最低版本 |
---|---|---|
groupName | 拉取群名称 | 1.4.0 |
userNickName | 用户昵称 | 1.9.90 |
userAvatarUrl | 用户头像 | 1.9.90 |
userGender | 用户性别 | 1.9.90 |
userCity | 用户所在城市 | 1.9.90 |
userProvince | 用户所在省份 | 1.9.90 |
userCountry | 用户所在国家 | 1.9.90 |
userLanguage | 用户的语言 | 1.9.90 |
lang 的合法值
值 | 说明 | 最低版本 |
---|---|---|
en | 英文 | |
zh_CN | 简体中文 | |
zh_TW | 繁体中文 |
Bug & Tip
tip
:只有当前用户在此群内才能拉取到群名称tip
:关于open-gid的获取请使用 wx.getShareInfo
示例代码
<open-data type="groupName" open-gid="xxxxxx"></open-data>
<open-data type="userAvatarUrl"></open-data>
<open-data type="userGender" lang="zh_CN"></open-data>
二、wx.request(Object object)
参数
Object object
属性 | 类型 | 默认值 | 必填 | 说明 | 最低版本 |
---|---|---|---|---|---|
url | string | 是 | 开发者服务器接口地址 | ||
data | string/object/ArrayBuffer | 否 | 请求的参数 | ||
header | Object | 否 | 设置请求的 header,header 中不能设置 Referer。content-type 默认为 application/json | ||
method | string | GET | 否 | HTTP 请求方法 | |
dataType | string | json | 否 | 返回的数据格式 | |
responseType | string | text | 否 | 响应的数据类型 | 1.7.0 |
success | function | 否 | 接口调用成功的回调函数 | ||
fail | function | 否 | 接口调用失败的回调函数 | ||
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
object.method 的合法值
值 | 说明 | 最低版本 |
---|---|---|
OPTIONS | HTTP 请求 OPTIONS | |
GET | HTTP 请求 GET | |
HEAD | HTTP 请求 HEAD | |
POST | HTTP 请求 POST | |
PUT | HTTP 请求 PUT | |
DELETE | HTTP 请求 DELETE | |
TRACE | HTTP 请求 TRACE | |
CONNECT | HTTP 请求 CONNECT |
object.dataType 的合法值
值 | 说明 | 最低版本 |
---|---|---|
json | 返回的数据为 JSON,返回后会对返回的数据进行一次 JSON.parse | |
其他 | 不对返回的内容进行 JSON.parse |
object.responseType 的合法值
值 | 说明 | 最低版本 |
---|---|---|
text | 响应的数据为文本 | |
arraybuffer | 响应的数据为 ArrayBuffer |
object.success 回调函数
参数
Object res
属性 | 类型 | 说明 | 最低版本 |
---|---|---|---|
data | string/Object/Arraybuffer | 开发者服务器返回的数据 | |
statusCode | number | 开发者服务器返回的 HTTP 状态码 | |
header | Object | 开发者服务器返回的 HTTP Response Header | 1.2.0 |
返回值
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: 'test.php', //仅为示例,并非真实的接口地址
data: {
x: '',
y: ''
},
header: {
'content-type': 'application/json' // 默认值
},
success (res) {
console.log(res.data)
}
})