Ajax 通過(guò)城市名獲取數(shù)據(jù)(全國(guó)天氣預(yù)報(bào)API)
預(yù)覽圖(比較簡(jiǎn)單粗糙)

聚合數(shù)據(jù)全國(guó)天氣預(yù)報(bào)接口:https://www.juhe.cn/docs/api/id/39
接口地址:http://v.juhe.cn/weather/index
支持格式:json/xml
請(qǐng)求方式:get
請(qǐng)求示例:http://v.juhe.cn/weather/index?format=2&cityname=%E8%8B%8F%E5%B7%9E&key=您申請(qǐng)的KEY
調(diào)用樣例及調(diào)試工具:API測(cè)試工具
請(qǐng)求參數(shù)說(shuō)明:
名稱(chēng) 類(lèi)型 必填 說(shuō)明
cityname string Y 城市名或城市ID,如:”蘇州”,需要utf8 urlencode
dtype string N 返回?cái)?shù)據(jù)格式:json或xml,默認(rèn)json
format int N 未來(lái)6天預(yù)報(bào)(future)兩種返回格式,1或2,默認(rèn)1
key string Y 你申請(qǐng)的key
HTML部分代碼:
<!DOCTYPE html> <html lang="en"> <meta charset="UTF-8" > <title>天氣預(yù)報(bào)</title> <script src="jquery-2.1.1.min.js"></script> <link rel="stylesheet" href="w.css"> <div id="mf_weather"> <script src="w.js"></script> <button id="search">天氣查詢(xún)</button> <input id="city" type="text" value="tbody"> <div class="ctn"> <div id="mufeng"> </div> <div id="future"></div> </div> </html>
JavaScript部分:
/*
* 1.輸入城市名
* 2,點(diǎn)擊的時(shí)候發(fā)送請(qǐng)求
* 3.響應(yīng)成功渲染頁(yè)面
* */
$('#search').on('click',function(){
var city = $('#city').val()||'北京';
$citycode=urlencode(city);
url='http://v.juhe.cn/weather/index?format=2&cityname='+$citycode+'&key=c82727e986a4f6cfc6ba1984f1f9183a';
$.ajax({url: url,
dataType: "jsonp",
type:"get",
data:{location:city},
success:function(data){
var sk = data.result.sk;
var today = data.result.today;
var futur = data.result.future;
var fut = "日期溫度天氣風(fēng)向";
$('#mufeng').html("<p>" + '當(dāng)前: ' + sk.temp + '℃ , ' + sk.wind_direction + sk.wind_strength + ' , ' + '空氣濕度' + sk.humidity + ' , 更新時(shí)間' + sk.time + "</p><p style='padding-bottom: 10px;'>" + today.city + ' 今天是: ' + today.date_y + ' ' + today.week + ' , ' + today.temperature + ' , ' + today.weather + ' , ' + today.wind + "<p></p>");
$('#future').html("<p>" + '未來(lái): ' + futur[0].temperature+ '℃ , ' + futur[0].weather + futur[0].wind + ' , ' + ' , 更新時(shí)間' + futur[0].week+futur[0].date + "</p><p style='padding-bottom: 10px;'>" + today.city + "<p></p>");
} });
});
function urlencode (str) {
str = (str + '').toString();
return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}
})
以上所述是小編給大家介紹的Ajax 通過(guò)城市名獲取數(shù)據(jù)(全國(guó)天氣預(yù)報(bào)API),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- PHP Ajax JavaScript Json獲取天氣信息實(shí)現(xiàn)代碼
- PHP+AJAX無(wú)刷新實(shí)現(xiàn)返回天氣預(yù)報(bào)數(shù)據(jù)
- JS HTML5 音樂(lè)天氣播放器(Ajax獲取天氣信息)
- AJAX集天氣\IP\多國(guó)語(yǔ)言翻譯MP3(可同步LRC歌詞顯示)\萬(wàn)年歷查詢(xún)通
- 天楓AJAX天氣預(yù)報(bào)系統(tǒng)V1.0
- 配合AJAX天氣預(yù)報(bào)的webService 之a(chǎn)sp
- AJAX天氣預(yù)報(bào)前臺(tái)
- ajax獲取用戶(hù)所在地天氣的方法
相關(guān)文章
ajax的get請(qǐng)求時(shí)緩存處理解決方法
這篇文章主要介紹了ajax的get請(qǐng)求時(shí)緩存處理解決方法,詳細(xì)分析了常見(jiàn)的四種解決方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-02-02
Ajax實(shí)現(xiàn)無(wú)刷新分頁(yè)實(shí)例代碼
這篇文章主要介紹了Ajax實(shí)現(xiàn)無(wú)刷新分頁(yè)實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
ajax響應(yīng)json字符串和json數(shù)組的實(shí)例(詳解)
下面小編就為大家?guī)?lái)一篇ajax響應(yīng)json字符串和json數(shù)組的實(shí)例(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
IE6中ajax aborted錯(cuò)誤請(qǐng)求中斷解決方法
給a標(biāo)簽綁定了一個(gè)click事件用來(lái)觸發(fā)ajax請(qǐng)求,在IE6中,請(qǐng)求時(shí)常會(huì)被中斷,在其他瀏覽器中都一切正常,具體解決方法如下,感興趣的朋友可以參考下2013-06-06
django獲取ajax的post復(fù)雜對(duì)象的實(shí)現(xiàn)方法
這篇文章主要介紹了django獲取ajax的post復(fù)雜對(duì)象的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-10-10
AJAX中同時(shí)發(fā)送多個(gè)請(qǐng)求XMLHttpRequest對(duì)象處理方法
AJAX中同時(shí)發(fā)送多個(gè)請(qǐng)求XMLHttpRequest對(duì)象處理方法...2007-04-04
AJAX如何接收J(rèn)SON數(shù)據(jù)示例介紹
如何使用AJAX返回JSON數(shù)據(jù),就是dataType,當(dāng)你設(shè)置json后返回的json字符串傳遞到客戶(hù)端就是JSON對(duì)象了,示例如下,感興趣的朋友可以參考下2013-08-08

