使用ajax獲取用戶所在地的天氣,供大家參考,具體內(nèi)容如下
1.要獲取用戶歸屬地的天氣,首先得獲取用戶所在的市區(qū),這里先獲取用戶的IP,通過IP獲取IP的歸屬地,從而得到用戶地址。
2.因?yàn)榘⒗镌铺峁┝送ㄟ^城市名(city)或者城市編號(hào)(cityId)
即可獲取天氣的API,從而獲取用戶歸屬地天氣
var city1;$.ajax({ //獲取客戶端 IP 和 歸屬地 url: "http://chaxun.1616.net/s.php?type=ip&output=json", dataType: "jsonp", success: function (data) { console.log('ip:' + data.Ip) console.log('歸屬地:' + data.Isp) var lcity = data.Isp.split(' ')[0]; //獲取短名稱,如淮安市 city1 = lcity.split('省')[1]; console.log(city1) //因?yàn)槭钱惒剿⑿拢詢蓚€(gè)請(qǐng)求幾乎同時(shí)進(jìn)行 $.ajax({ type: 'get', url: 'http://jisutqybmf.market.alicloudapi.com/weather/query', async: true, //設(shè)置驗(yàn)證方式,設(shè)置請(qǐng)求頭 //1,APPCode headers: { Authorization: "APPCODE 你的APPCode" }, //2.APPSecret 暫時(shí)不能用 //headers: { AppKey: '你的APPKey', AppSecret :'你的APPSecret' }, data: { city: city1 }, success: function (result) { console.log(result['result']) //alert(result) }, error: function () { alert('error') } }); }});
輸出結(jié)果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選