- 开发无止境 -
Data: 2021-09-01 07:22:06Form: JournalClick: 18
请求参数
编号 | 名称 | 必填 | 类型 | 说明 |
---|---|---|---|---|
1 | city | 是 | string | 要查询的城市名称/id,城市名称如:温州、上海、北京,需要 utf8 urlencode |
2 | key | 是 | tring | 在个人中心->我的数据,接口名称上方查看 |
返回参数
编号 | 名称 | 类型 | 说明 |
---|---|---|---|
1 | error_code | int | 返回码,0 为查询成功 |
2 | reason | string | 返回说明 |
3 | result | string | 返回结果集 |
- | - | - | - |
4 | realtime | - | 当前天气详情情况 |
5 | info | string | 天气情况,如:晴、多云 |
6 | wid | string | 天气标识 id,可参考小接口 2 |
7 | temperature | string | 温度,可能为空 |
8 | humidity | string | 湿度,可能为空 |
9 | direct | string | 风向,可能为空 |
10 | power | string | 风力,可能为空 |
11 | aqi | string | 空气质量指数,可能为空 |
- | - | - | - |
12 | future | - | 近 5 天天气情况 |
13 | date | string | 日期 |
14 | temperature | string | 温度,最低温/最高温 |
15 | weather | string | 天气情况 |
16 | direct | string | 风向 |
JSON 返回示例
{
"error_code": 0,
"reason": "查询成功",
"result": {
"city": "苏州",
"realtime": {
"info": "阴",
"wid": "02",
"temperature": "4",
"humidity": "82",
"direct": "西北风",
"power": "3级",
"aqi": "80"
},
"future": [
{
"date": "2019-02-22",
"temperature": "1/7℃",
"weather": "小雨转多云",
"wid": {
"day": "07",
"night": "01"
},
"direct": "北风转西北风"
},
{
"date": "2019-02-23",
"temperature": "2/11℃",
"weather": "多云转阴",
"wid": {
"day": "01",
"night": "02"
},
"direct": "北风转东北风"
},
{
"date": "2019-02-24",
"temperature": "6/12℃",
"weather": "多云",
"wid": {
"day": "01",
"night": "01"
},
"direct": "东北风转北风"
},
{
"date": "2019-02-25",
"temperature": "5/12℃",
"weather": "小雨转多云",
"wid": {
"day": "07",
"night": "01"
},
"direct": "东北风"
},
{
"date": "2019-02-26",
"temperature": "5/11℃",
"weather": "多云转小雨",
"wid": {
"day": "01",
"night": "07"
},
"direct": "东北风"
}
]
}
}
wx.request
编号 | 属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|---|
1 | url | string | 是 | 开发者服务器接口地址 | |
2 | data | string/object/ArrayBuffer | 否 | 请求的参数 | |
3 | header | Object | 否 | 设置请求的 header,header 中不能设置 Referer。content-type 默认为 application/json | |
4 | timeout | number | 否 | 超时时间,单位为毫秒 | |
5 | method | string | GET | 否 | HTTP 请求方法 |
6 | dataType | string | json | 否 | 返回的数据格式 |
7 | responseType | string | text | 否 | 响应的数据类型 |
8 | success | function | 否 | 接口调用成功的回调函数 | |
9 | fail | function | 否 | 接口调用失败的回调函数 | |
10 | complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
编号 | 属性 | 类型 | 说明 |
---|---|---|---|
1 | data | string/Object/Arraybuffer | 开发者服务器返回的数据 |
2 | statusCode | number | 开发者服务器返回的 HTTP 状态码 |
3 | header | Object | 开发者服务器返回的 HTTP Response Header |
4 | cookies | Array.<string> | 开发者服务器返回的 cookies,格式为字符串数组 |
Page({
onLoad() {
wx.request({
url: "http://apis.juhe.cn/simpleWeather/query",
data: {
city: "合肥",
key: "abc4b64ae7656b460723402175a5650b",
},
success(res) {
console.log(res);
},
});
},
});
method 的合法值
编号 | 值 | 说明 |
---|---|---|
1 | GET | HTTP 请求 GET |
2 | POST | HTTP 请求 POST |
3 | HEAD | HTTP 请求 HEAD |
4 | PUT | HTTP 请求 PUT |
5 | DELETE | HTTP 请求 DELETE |
6 | TRACE | HTTP 请求 TRACE |
7 | CONNECT | HTTP 请求 CONNECT |
8 | OPTIONS | HTTP 请求 OPTIONS |
dataType 的合法值
编号 | 值 | 说明 |
---|---|---|
1 | json | 返回的数据为 JSON,返回后会对返回的数据进行一次 JSON.parse |
2 | 其他 | 不对返回的内容进行 JSON.parse |
responseType 的合法值
编号 | 值 | 说明 |
---|---|---|
1 | text | 响应的数据为文本 |
2 | arraybuffer | 响应的数据为 ArrayBuffer |
Page({
onShow() {
wx.request({
url: "http://apis.juhe.cn/simpleWeather/query",
data: {
city: "合肥",
key: "abc4b64ae7656b460723402175a5650b",
},
header: {
// 'content-type': 'application/json'
"content-type": "application/x-www-form-urlencoded",
},
method: "POST",
dataType: "json",
responseType: "text",
success(res) {
console.log(res);
},
});
},
});
data
参数:最终发送给服务器的数据是 String
类型,如果传入的 data
不是 String
类型,会被转换成 String