当前位置:编程学习 > JS >>

二款异步调用天气实例代码

本文章提供二款异步调用天气实例代码一款是利用js ajax异步调用另一种是利用jquery ajax 异步调用哦。

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.zzzyk.com/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>jquery js 异步调用天气实例代码</title>
<script language="网页特效">
//js异步调用天气
function getjsfile(url, callback){
var xh = window.xmlhttprequest ? new xmlhttprequest : new activexobject('msxml2.xmlhttp');
xh.open('get',url,true);
xh.onreadystatechange = function(){
if(xh.readystate == 4 && xh.status == 200){
if(window.execscript) window.execscript(xh.responsetext);
else eval.call(window, xh.responsetext);
eval(callback)();
}
}
xh.send('');
}
//jquery异步调用天气

$.ajax({type:"get", url: "http://m.weather.com.cn/data/101210401.html", datatype: "html",
success: function(json){
alert(json);
var t = '('+json+')';
alert(t);
var result = eval(t);
//var result = eval("{" + json + "}");
//alert(json.responsetext);
alert(result);
$("#weather").append(result.weatherinfo.city+' <img src="http://m.weather.com.cn/img/c'+result.weatherinfo.img1+'.gif"/>'+' <img src="http://m.weather.com.cn/img/c'+result.weatherinfo.img2+'.gif"/> '+result.weatherinfo.weather1+' '+ result.weatherinfo.temp1);
}
});

方法三

function page_init() {
            try {
                $.get("asp教程x?cam_id=novell20100608&msg={count">http://fff.ff.com/responsevalue.aspx?cam_id=novell20100608&msg={count}", null, getcallback, 'json');
            } catch (e) {

            }

        }
        function getcallback(response) {
            if (typeof (response) != "object") {
                $('#persons').html('0');
                return false;
            }
            if (response.error == 1) {
                //alert(response.msg);
                $('#persons').html(response.msg);
                return;
            }

        }

调用方法:

$(document).ready(
function() {
      page_init();
});

</script>
</head>

<body>
</body>
</html>

补充:网页制作,jquery 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,