透过城市名获取数据,web天气预告

急需安不忘忧:jQuery Bootstrap 天气预先报告API(本文中利用API也许会失灵请灵活运用)

亟需准备:jQuery Bootstrap 天气预先报告API(本文中采用API大概会失灵请灵活运用)

WEB前端工程师(实行)制作天气预告难度:轻巧,web天气预先报告

亟待预加防止:jQuery Bootstrap 天气预告API(本文中动用API或者会失灵请灵活运用)

CSS样式能够你协调去写那里只提议jQuery 请求数据和解析JSON数据 

{
 "resultcode":"200",
 "reason":"successed!",
 "result":{
  "sk":{
   "temp":"21",
   "wind_direction":"东北风",
   "wind_strength":"3级",
   "humidity":"66%",
   "time":"14:00"
  },
  "today":{
   "temperature":"17℃~22℃",
   "weather":"阴",
   "weather_id":{
    "fa":"02",
    "fb":"02"
   },
   "wind":"东北风3-4 级",
   "week":"星期三",
   "city":"无锡",
   "date_y":"2016年10月12日",
   "dressing_index":"较舒适",
   "dressing_advice":"建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。",
   "uv_index":"最弱",
   "comfort_index":"",
   "wash_index":"较适宜",
   "travel_index":"较适宜",
   "exercise_index":"较适宜",
   "drying_index":""
  },
  "future":{
   "day_20161012":{
    "temperature":"17℃~22℃",
    "weather":"阴",
    "weather_id":{
     "fa":"02",
     "fb":"02"
    },
    "wind":"东北风3-4 级",
    "week":"星期三",
    "date":"20161012"
   },
   "day_20161013":{
    "temperature":"17℃~21℃",
    "weather":"阴",
    "weather_id":{
     "fa":"02",
     "fb":"02"
    },
    "wind":"东北风3-4 级",
    "week":"星期四",
    "date":"20161013"
   },
   "day_20161014":{
    "temperature":"18℃~22℃",
    "weather":"阵雨",
    "weather_id":{
     "fa":"03",
     "fb":"03"
    },
    "wind":"东北风3-4 级",
    "week":"星期五",
    "date":"20161014"
   },
   "day_20161015":{
    "temperature":"17℃~22℃",
    "weather":"阵雨",
    "weather_id":{
     "fa":"03",
     "fb":"03"
    },
    "wind":"东北风3-4 级",
    "week":"星期六",
    "date":"20161015"
   },
   "day_20161016":{
    "temperature":"17℃~24℃",
    "weather":"阴",
    "weather_id":{
     "fa":"02",
     "fb":"02"
    },
    "wind":"东北风3-4 级",
    "week":"星期日",
    "date":"20161016"
   },
   "day_20161017":{
    "temperature":"17℃~21℃",
    "weather":"阴",
    "weather_id":{
     "fa":"02",
     "fb":"02"
    },
    "wind":"东北风3-4 级",
    "week":"星期一",
    "date":"20161017"
   },
   "day_20161018":{
    "temperature":"17℃~21℃",
    "weather":"阴",
    "weather_id":{
     "fa":"02",
     "fb":"02"
    },
    "wind":"东北风3-4 级",
    "week":"星期二",
    "date":"20161018"
   }
  }
 },
 "error_code":0
}

  

地点是要用到的JSON数据格式

搭建分界面略过 根据JSON中数量增加到HTML中

 

浅析数据到HTML中 当中必然要小心 跨域难点

$(document).ready(function() {
$.ajax({
type : “POST”,
dataType:’JSONP’,//跨域
jsonp:”callback”,
jsonpCallback:”fanyi”,
url:”
data :
{ip:”1二肆.1二陆.230.180″,key:”b2a20八cb3玖cec一c九3dd55349667082八伍”},//请求数据
success : function(datas){//datas是重返的JSON数据

if (datas.resultcode == 200) {//依照重回的数据
判定是不是成功得到到JSON中的值
$(“#city”).text(datas.result.today.city);//解析数据到HTML文书档案中展示数据
$(“#date”).text(datas.result.sk.time+”发布”);
$(“#temp”).text(datas.result.sk.temp+”°”);
$(“#weather”).text(datas.result.today.weather);
$(“#jt-tq”).text(datas.result.today.weather);
$(“#jt-wd”).text(datas.result.today.temperature);
$(“#jt-fx”).text(datas.result.today.wind);
$(“#two-week”).text(datas.result.future.day_20161013.week);
$(“#two-tq”).text(datas.result.future.day_20161013.weather);
$(“#two-wd”).text(datas.result.future.day_20161013.temperature);
$(“#two-fx”).text(datas.result.future.day_20161013.wind);
$(“#three-week”).text(datas.result.future.day_透过城市名获取数据,web天气预告。20161014.week);
$(“#three-tq”).text(datas.result.future.day_20161014.weather);
$(“#three-wd”).text(datas.result.future.day_20161014.temperature);
$(“#three-fx”).text(datas.result.future.day_20161014.wind);

}else{

//错误…

}

}
});
});

 

 

 

要求早为之所:jQuery Bootstrap 天气预告API(本文中使用API恐怕会失效请灵活运用)
CSS样式可…

Ajax 通过城市名获取数据(全国天气预先报告API),ajaxapi

预览图(相比较简单粗糙)

亚洲必赢app 1

集合数据全国天气预先报告接口:

亚洲必赢app ,接口地址:
接济格式:json/xml
伸手情势:get
呼吁示例:
调用样例及调节和测试工具:API测试工具
恳请参数表达:
名称 类型 必填 说明
cityname string Y 城市名或城市ID,如:”埃德蒙顿”,需求utf八 urlencode
dtype string N 重回数据格式:json或xml,暗中同意json
format int N 以往陆天预先报告(future)三种回到格式,一或2,暗许一
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">
<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部分:

/*
* 一.输入城市名
* 贰,点击的时候发送请求
* 三.响应成功渲染页面
* */
$(‘#search’).on(‘click’,function(){
var city = $(‘#city’).val()||’北京’;
$citycode=urlencode(city);
url=”;
$.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: 十px;’>” + 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
通过城市名获取数据(全国天气预先报告API),希望对大家有所帮忙,若是大家有别的疑问请给作者留言,笔者会及时还原咱们的。在此也卓殊多谢我们对帮客之家网址的补助!

通过城市名获取数据(全国天气预先报告API),ajaxapi
预览图(相比较轻松粗糙)
聚合数据全国天气预先报告接口:
接口地…

CSS样式能够你协调去写那里只提议jQuery 请求数据和平解决析JSON数据 

CSS样式能够你协调去写那里只建议jQuery 请求数据和平解决析JSON数据 

{
 "resultcode":"200",
 "reason":"successed!",
 "result":{
  "sk":{
   "temp":"21",
   "wind_direction":"东北风",
   "wind_strength":"3级",
   "humidity":"66%",
   "time":"14:00"
  },
  "today":{
   "temperature":"17℃~22℃",
   "weather":"阴",
   "weather_id":{
    "fa":"02",
    "fb":"02"
   },
   "wind":"东北风3-4 级",
   "week":"星期三",
   "city":"无锡",
   "date_y":"2016年10月12日",
   "dressing_index":"较舒适",
   "dressing_advice":"建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。",
   "uv_index":"最弱",
   "comfort_index":"",
   "wash_index":"较适宜",
   "travel_index":"较适宜",
   "exercise_index":"较适宜",
   "drying_index":""
  },
  "future":{
   "day_20161012":{
    "temperature":"17℃~22℃",
    "weather":"阴",
    "weather_id":{
     "fa":"02",
     "fb":"02"
    },
    "wind":"东北风3-4 级",
    "week":"星期三",
    "date":"20161012"
   },
   "day_20161013":{
    "temperature":"17℃~21℃",
    "weather":"阴",
    "weather_id":{
     "fa":"02",
     "fb":"02"
    },
    "wind":"东北风3-4 级",
    "week":"星期四",
    "date":"20161013"
   },
   "day_20161014":{
    "temperature":"18℃~22℃",
    "weather":"阵雨",
    "weather_id":{
     "fa":"03",
     "fb":"03"
    },
    "wind":"东北风3-4 级",
    "week":"星期五",
    "date":"20161014"
   },
   "day_20161015":{
    "temperature":"17℃~22℃",
    "weather":"阵雨",
    "weather_id":{
     "fa":"03",
     "fb":"03"
    },
    "wind":"东北风3-4 级",
    "week":"星期六",
    "date":"20161015"
   },
   "day_20161016":{
    "temperature":"17℃~24℃",
    "weather":"阴",
    "weather_id":{
     "fa":"02",
     "fb":"02"
    },
    "wind":"东北风3-4 级",
    "week":"星期日",
    "date":"20161016"
   },
   "day_20161017":{
    "temperature":"17℃~21℃",
    "weather":"阴",
    "weather_id":{
     "fa":"02",
     "fb":"02"
    },
    "wind":"东北风3-4 级",
    "week":"星期一",
    "date":"20161017"
   },
   "day_20161018":{
    "temperature":"17℃~21℃",
    "weather":"阴",
    "weather_id":{
     "fa":"02",
     "fb":"02"
    },
    "wind":"东北风3-4 级",
    "week":"星期二",
    "date":"20161018"
   }
  }
 },
 "error_code":0
}
{
    "resultcode":"200",
    "reason":"successed!",
    "result":{
        "sk":{
            "temp":"21",
            "wind_direction":"东北风",
            "wind_strength":"3级",
            "humidity":"66%",
            "time":"14:00"
        },
        "today":{
            "temperature":"17℃~22℃",
            "weather":"阴",
            "weather_id":{
                "fa":"02",
                "fb":"02"
            },
            "wind":"东北风3-4 级",
            "week":"星期三",
            "city":"无锡",
            "date_y":"2016年10月12日",
            "dressing_index":"较舒适",
            "dressing_advice":"建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。",
            "uv_index":"最弱",
            "comfort_index":"",
            "wash_index":"较适宜",
            "travel_index":"较适宜",
            "exercise_index":"较适宜",
            "drying_index":""
        },
        "future":{
            "day_20161012":{
                "temperature":"17℃~22℃",
                "weather":"阴",
                "weather_id":{
                    "fa":"02",
                    "fb":"02"
                },
                "wind":"东北风3-4 级",
                "week":"星期三",
                "date":"20161012"
            },
            "day_20161013":{
                "temperature":"17℃~21℃",
                "weather":"阴",
                "weather_id":{
                    "fa":"02",
                    "fb":"02"
                },
                "wind":"东北风3-4 级",
                "week":"星期四",
                "date":"20161013"
            },
            "day_20161014":{
                "temperature":"18℃~22℃",
                "weather":"阵雨",
                "weather_id":{
                    "fa":"03",
                    "fb":"03"
                },
                "wind":"东北风3-4 级",
                "week":"星期五",
                "date":"20161014"
            },
            "day_20161015":{
                "temperature":"17℃~22℃",
                "weather":"阵雨",
                "weather_id":{
                    "fa":"03",
                    "fb":"03"
                },
                "wind":"东北风3-4 级",
                "week":"星期六",
                "date":"20161015"
            },
            "day_20161016":{
                "temperature":"17℃~24℃",
                "weather":"阴",
                "weather_id":{
                    "fa":"02",
                    "fb":"02"
                },
                "wind":"东北风3-4 级",
                "week":"星期日",
                "date":"20161016"
            },
            "day_20161017":{
                "temperature":"17℃~21℃",
                "weather":"阴",
                "weather_id":{
                    "fa":"02",
                    "fb":"02"
                },
                "wind":"东北风3-4 级",
                "week":"星期一",
                "date":"20161017"
            },
            "day_20161018":{
                "temperature":"17℃~21℃",
                "weather":"阴",
                "weather_id":{
                    "fa":"02",
                    "fb":"02"
                },
                "wind":"东北风3-4 级",
                "week":"星期二",
                "date":"20161018"
            }
        }
    },
    "error_code":0
}

  

  

地方是要用到的JSON数据格式

地方是要用到的JSON数据格式

搭建分界面略过 依照JSON中数据增进到HTML中

搭建界面略过 依据JSON中多少增加到HTML中

 

 

剖析数据到HTML中 当中明确要小心 跨域难题

分析数据到HTML中 个中鲜明要注意 跨域难点

$(document).ready(function() {
$.ajax({
type : “POST”,
dataType:’JSONP’,//跨域
jsonp:”callback”,
jsonpCallback:”fanyi”,
url:”
data :
{ip:”1二四.12陆.230.180″,key:”b二a208cb3玖cec1c九三dd553496670828五”},//请求数据
success : function(datas){//datas是回去的JSON数据

$(document).ready(function() {
$.ajax({
type : “POST”,
dataType:’JSONP’,//跨域
jsonp:”callback”,
jsonpCallback:”fanyi”,
url:”
data :
{ip:”1贰四.126.230.180″,key:”b二a208cb3九cec1c玖叁dd55349667082捌5″},//请求数据
success : function(datas){//datas是回来的JSON数据

if (datas.resultcode == 200) {//依照重临的数据
决断是还是不是中标博得到JSON中的值
$(“#city”).text(datas.result.today.city);//解析数据到HTML文书档案中展示数据
$(“#date”).text(datas.result.sk.time+”发布”);
$(“#temp”).text(datas.result.sk.temp+”°”);
$(“#weather”).text(datas.result.today.weather);
$(“#jt-tq”).text(datas.result.today.weather);
$(“#jt-wd”).text(datas.result.today.temperature);
$(“#jt-fx”).text(datas.result.today.wind);
$(“#two-week”).text(datas.result.future.day_20161013.week);
$(“#two-tq”).text(datas.result.future.day_20161013.weather);
$(“#two-wd”).text(datas.result.future.day_20161013.temperature);
$(“#two-fx”).text(datas.result.future.day_20161013.wind);
$(“#three-week”).text(datas.result.future.day_20161014.week);
$(“#three-tq”).text(datas.result.future.day_20161014.weather);
$(“#three-wd”).text(datas.result.future.day_20161014.temperature);
$(“#three-fx”).text(datas.result.future.day_20161014.wind);

if (datas.resultcode == 200) {//根据重返的数据
判别是或不是中标收获到JSON中的值
$(“#city”).text(datas.result.today.city);//解析数据到HTML文书档案中显得数据
$(“#date”).text(datas.result.sk.time+”发布”);
$(“#temp”).text(datas.result.sk.temp+”°”);
$(“#weather”).text(datas.result.today.weather);
$(“#jt-tq”).text(datas.result.today.weather);
$(“#jt-wd”).text(datas.result.today.temperature);
$(“#jt-fx”).text(datas.result.today.wind);
$(“#two-week”).text(datas.result.future.day_20161013.week);
$(“#two-tq”).text(datas.result.future.day_20161013.weather);
$(“#two-wd”).text(datas.result.future.day_20161013.temperature);
$(“#two-fx”).text(datas.result.future.day_20161013.wind);
$(“#three-week”).text(datas.result.future.day_20161014.week);
$(“#three-tq”).text(datas.result.future.day_20161014.weather);
$(“#three-wd”).text(datas.result.future.day_20161014.temperature);
$(“#three-fx”).text(datas.result.future.day_20161014.wind);

}else{

}else{

//错误…

//错误…

}

}

}
});
});

}
});
});

 

 

 

 

 

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图