国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > AJAX > 正文

Ajax 通過(guò)城市名獲取數(shù)據(jù)(全國(guó)天氣預(yù)報(bào)API)

2024-09-01 08:27:39
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

預(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ì)錯(cuò)新站長(zhǎng)站網(wǎng)站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 当涂县| 乌兰浩特市| 岳阳县| 牟定县| 仁布县| 华蓥市| 庄河市| 黑山县| 莫力| 会同县| 南木林县| 连山| 永善县| 芒康县| 江北区| 白山市| 西贡区| 五家渠市| 盐津县| 温州市| 桑日县| 大城县| 扶风县| 临泽县| 新巴尔虎左旗| 楚雄市| 黄平县| 丹凤县| 庄浪县| 新建县| 定安县| 巴中市| 金寨县| 彩票| 洮南市| 衡东县| 兰坪| 化隆| 泽库县| 桂东县| 监利县|