Ajax 通過城市名獲取數(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ù)說明:
名稱 類型 必填 說明
cityname string Y 城市名或城市ID,如:”蘇州”,需要utf8 urlencode
dtype string N 返回?cái)?shù)據(jù)格式:json或xml,默認(rèn)json
format int N 未來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">天氣查詢</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>" + '未來: ' + 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 通過城市名獲取數(shù)據(jù)(全國(guó)天氣預(yù)報(bào)API),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
php ajax網(wǎng)站瀏覽統(tǒng)計(jì)功能的簡(jiǎn)單實(shí)現(xiàn)
這個(gè)功能應(yīng)該是很多網(wǎng)站都需要的,這里僅僅實(shí)現(xiàn)了一個(gè)基于文件的簡(jiǎn)易版本,數(shù)據(jù)庫(kù)的版本請(qǐng)自行參考實(shí)現(xiàn),我這里實(shí)現(xiàn)的功能很不完善,比如未過濾是否為同一訪客,是否為同一IP等等,這里僅僅是給大家提供一個(gè)參考.2008-09-09菜鳥蔡之Ajax復(fù)習(xí)第一篇(后臺(tái)asp.net)(傳統(tǒng)的JavaScript方法實(shí)現(xiàn)Ajax功能)
Ajax是Asynchronous JavaScript and XML,其核心是通過XMLHttpRequest對(duì)象以一種異步的方式向服務(wù)器發(fā)送請(qǐng)求,并通過該對(duì)象接收請(qǐng)求返回的數(shù)據(jù),從而完成人機(jī)交互的數(shù)據(jù)操作(呵呵、、、說的有點(diǎn)嚇人!)2012-11-11Ajax實(shí)現(xiàn)的異步傳輸與驗(yàn)證示例代碼
Ajax異步傳輸應(yīng)用很廣當(dāng)用戶注冊(cè)時(shí),當(dāng)用戶剛一輸完,立即判斷用戶是否存在這就用到了異步傳輸2014-01-01ajax實(shí)時(shí)任務(wù)提示功能的實(shí)現(xiàn)代碼
本項(xiàng)目運(yùn)用了 FLEAPHP,MYSQL,SMARTY,FCKEDItor,JSON,PROTOTYPE的技術(shù),在這里首先要感謝這些開源項(xiàng)目的開發(fā)者給我們帶來的好東西,其次要感謝[生氣豬--讓我?guī)退鲆粋€(gè)這樣的小東西來提醒她按時(shí)完成事情].花了一個(gè)3個(gè)小時(shí)完成.希望給大家起到拋磚引玉的作用啊....2008-09-09Ajax請(qǐng)求成功后return無法接收到返回值的問題及解決方案
項(xiàng)目中需要頻繁的用到ajax請(qǐng)求,所以就想用一個(gè)方法將整個(gè)請(qǐng)求封裝起來,直接調(diào)用方法傳遞參數(shù),然后返回請(qǐng)求結(jié)果就可以了,這篇文章主要介紹了ajax請(qǐng)求成功后return無法接收到返回值,需要的朋友可以參考下2023-10-10