最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

Ajax通过城市名获取数据

来源:动视网 责编:小采 时间:2020-11-27 19:47:43
文档

Ajax通过城市名获取数据

Ajax通过城市名获取数据:本文给大家分享全国天气预报API Ajax 通过城市名获取数据,通过html和js两部分代码实现天气预报效果,输入城市就会出现天气情况,效果非常棒,感兴趣的朋友可以参考下预览图(比较简单粗糙)聚合数据全国天气预报接口:https://www.juhe.cn/docs
推荐度:
导读Ajax通过城市名获取数据:本文给大家分享全国天气预报API Ajax 通过城市名获取数据,通过html和js两部分代码实现天气预报效果,输入城市就会出现天气情况,效果非常棒,感兴趣的朋友可以参考下预览图(比较简单粗糙)聚合数据全国天气预报接口:https://www.juhe.cn/docs
 本文给大家分享全国天气预报API Ajax 通过城市名获取数据,通过html和js两部分代码实现天气预报效果,输入城市就会出现天气情况,效果非常棒,感兴趣的朋友可以参考下

预览图(比较简单粗糙)

聚合数据全国天气预报接口:https://www.juhe.cn/docs/api/id/39

接口地址:http://v.juhe.cn/weather/index
支持格式:json/xml
请求方式:get
请求示例:http://v.juhe.cn/weather/index?format=2&cityname=%E8%8B%8F%E5%B7%9E&key=您申请的KEY
调用样例及调试工具:API测试工具
请求参数说明:
名称 类型 必填 说明
cityname string Y 城市名或城市ID,如:”苏州”,需要utf8 urlencode
dtype string N 返回数据格式:json或xml,默认json
format int N 未来6天预报(future)两种返回格式,1或2,默认1
key string Y 你申请的key

HTML部分代码:

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8" >
<title>天气预报</title>
<script src="jquery-2.1.1.min.js"></script>
<link rel="stylesheet" href="w.css">
<p id="mf_weather">
<script src="w.js"></script>
<button id="search">天气查询</button>
<input id="city" type="text" value="tbody">
<p class="ctn">
<p id="mufeng">
</p>
<p id="future"></p>
</p> 
</html>

JavaScript部分:

/*
* 1.输入城市名
* 2,点击的时候发送请求
* 3.响应成功渲染页面
* */
$('#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 = "日期温度天气风向";
$('#mufeng').html("<p>" + '当前: ' + sk.temp + '℃ , ' + sk.wind_direction + sk.wind_strength + ' , ' + '空气湿度' + sk.humidity + ' , 更新时间' + 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 + ' , ' + ' , 更新时间' + 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动态为下拉列表添加数据的实现方法

完美解决ajax跨域请求下parsererror的错误

ajax提交手机号去数据库验证并返回状态值

文档

Ajax通过城市名获取数据

Ajax通过城市名获取数据:本文给大家分享全国天气预报API Ajax 通过城市名获取数据,通过html和js两部分代码实现天气预报效果,输入城市就会出现天气情况,效果非常棒,感兴趣的朋友可以参考下预览图(比较简单粗糙)聚合数据全国天气预报接口:https://www.juhe.cn/docs
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top