使用 Ajax 获取天气预报的数据


var ourXHR ;
function Sky(city) {
var uri = "http://wthrcdn.etouch.cn/weather_mini?city="+city;
ourXHR = null;
if(window.XMLHttpRequest){
ourXHR = new XMLHttpRequest();
}else if(window.ActiveXObject){
ourXHR = new ActiveXObject("Microsoft.XMLHTTP");
}else{
return "error: 该浏览器版本太低!!!";
}
ourXHR.open('GET',uri,true);
ourXHR.onreadystatechange = function () {
if (ourXHR.readyState==4&&ourXHR.status==200){
var weather = JSON.parse(ourXHR.responseText).data;
// 呈现数据
showWeather(weather);
}
};
ourXHR.send();
}

function showWeather(data) {
var city = data.city,
fiveDay = data.forecast,
weather = document.getElementById('weather');
var str = "日期最高气温最低气温风向天气"
+""+city+" 天气预报";
for(var j in fiveDay){
str+=""+fiveDay[j].date+""+fiveDay[j].high+""+fiveDay[j].low+
    ""+fiveDay[j].fengxiang+""+fiveDay[j].type+"";
}
weather.innerHTML = str;
}
Sky('广州');

 

 

获取的数据

使用 Ajax 获取天气预报的数据_第1张图片

 

你可能感兴趣的:(JavaScript)