1.新浪調用天氣預報的方法,代碼如下
//天氣預報
function findWeather() {
var cityUrl = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js';
$.getScript(cityUrl, function(script, textStatus, jqXHR) {
var citytq = remote_ip_info.city ;// 擷取城市
var url = "http://php.weather.sina.com.cn/iframe/index/w_cl.php?code=js&city=" + citytq + "&day=0&dfc=3&charset=utf-8";
$.ajax({
url:url,
dataType:"script",
success : function(data) {
var _w = window.SWther.w[citytq][0];
var _f= _w.f1+"_0.png";
if(new Date().getHours() > 17){
_f= _w.f2+"_1.png";
}
var img = "<img width='70px' height='60px' src='http://i2.sinaimg.cn/dy/main/weather/weatherplugin/wthIco/20_20/" +_f
+ "' />";
var tq = citytq + " " + img + " " + _w.s1 + " " + _w.t1 + "℃~" + _w.t2 + "℃ " + _w.d1 + _w.p1 + "級";
//#weather自己修改,天氣樣式自己打斷點調試
alert(tq);
}
});
});
}
$(document).ready(function (){
var city = '内蒙古';
$.getScript("http://php.weather.sina.com.cn/iframe/index/w_cl.php?code=js&day=2&city="+city+"&dfc=3",function(){
alert(window.SWther.w[city][0].s1);
})
});
2.正常錯誤說明
我在調用時window.SWther.w[citytq][0]取值一直為undefined。經查明是由于頁面編碼格式導緻,新浪天氣接口預設應該是gbk,我的頁面預設是UTF-8,是以在這種情況下,講請求連結後加入&charset=utf-8即可解決。