Finology 大数据金融

通过大数据以量化金融

Promise最大的好处是在异步执行过程中,把执行代码逻辑和处理请求后的结果分离了,解偶合了。

执行代码逻辑,比如请求api后,返回http code 200怎么做,返回400又怎么做。

可以把如下代码复制到Chrome的Console里面执行。

1
2
3
4
5
6
7
8
9
10
11
new Promise(function(resolve, reject) {
var return_code = 400 // 任意设置
if (return_code == 200) {
resolve('OK')
} else {
reject({code: 400})
}
}).then((res) => {
console.log(res)
}).catch((res) => {
console.log(res)});

高德开放平台下的天气查询接口文档

https://lbs.amap.com/api/webservice/guide/api/weatherinfo

注册开发者账号,获取Key

1
https://restapi.amap.com/v3/weather/weatherInfo?parameters

parameters代表的参数包括必填参数和可选参数。

https://restapi.amap.com/v3/weather/weatherInfo?key=[key]&city=310115

返回结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
"status": "1",
"count": "1",
"info": "OK",
"infocode": "10000",
"lives": [
{
"province": "上海",
"city": "浦东新区",
"adcode": "310115",
"weather": "多云",
"temperature": "6",
"winddirection": "北",
"windpower": "≤3",
"humidity": "56",
"reporttime": "2020-01-12 17:57:21"
}
]
}

百度api是jsonp的方式获取数据,而高德地图的api是允许跨域的。

获取天气查询服务的文档地址

http://lbsyun.baidu.com/index.php?title=car/api/weather

首先得拿到到ak参数的值,也就是密钥。

点击”获取密钥”链接,进入页面,注册成为百度地图开放平台开发者。

获取密钥

查询api:

http://api.map.baidu.com/telematics/v3/weather?location=上海&output=json&ak=[ak密钥]

这个api,已经不开放给新注册的用户了。

如果是新注册的开发者用户,得看看我下一篇文章了,换成高德开放平台了。

高德开放平台天气查询API
0%