1. 程式人生 > >ajax 非同步封裝-函式 javascript原生

ajax 非同步封裝-函式 javascript原生

以下為簡單的案列程式碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文件</title>
<script type="text/javascript" src="Scripts/jquery.js"></script>
</head>
<style>
* { margin: 0px; padding: 0px; }
#box { float: left; width: 500px; }
#left { float: left; background: #090; width: 100px; height: 100px; }
#right { background: #C60; width: 100px; height: 100px; float: left; }
#box2 { width: 180px; height: 100px; }
html>body #box2 { width: auto; height: auto; min-width: 180px; min-height: 100px; }
</style>
<body>
<div id="box">
  <div id="left">點選我 看效果!</div>
  <div id="right">fffeeee</div>
</div>
<div style="width:100px; height:100px; background:#969; float:left;" id="dd">dddd</div>
<script>
// 非同步請求封裝 IE6即以上瀏覽器
    // ajax(url,fnSucc,selectID,fnFaild)
   //url 請求地址
   //fnSucc 非同步請求後的內容處理函式
   //fnFaild 請求失敗處理函式
   
 function ajax(url,fnSucc,fnFaild)
                {   
                                //1.建立Ajax物件 
                                //非IE6
                                var oAjax;
                                if(window.XMLHttpRequest)//不會報錯,只會是undefined
                                        {oAjax=new XMLHttpRequest();}
                                else
                                //iE6 IE5
                                        {oAjax=new ActiveXObject("Microsoft.XMLHTTP");}
                                //alert(oAjax);
                                //2.連線伺服器
                                //open(方法,檔名,非同步傳輸)
                                oAjax.open("get",url,true);//制止快取
                                //3.傳送請求
                                oAjax.send();
                                //4.接收返回值  和伺服器通訊的時候此事件發生
                                oAjax.onreadystatechange=function()
                                {
                                        //oAjax.readyState //瀏覽器和伺服器,進行到哪一步了 非同步握手過程
                                        if(oAjax.readyState==4)//讀取完成(可能檔案不存在)
                                        {
                                                if(oAjax.status==200 || oAjax.status==304)//請求成功  304即使瀏覽器快取了也返回資料
                                                {
                                                        fnSucc(oAjax.responseText);
                                                        //alert("成功"+oAjax.responseText);
                                                }
                                                else
                                                {
                                                        if(fnFaild)//fnFaild傳進來時
                                                        {
                                                                fnFaild(oAjax.status);
                                                        }
                                                        //alert("失敗:"+oAjax.status);//status為404
                                                }
                                        }
                                }
                       
                }
				
	window.onload=function(){
	var oBtn=document.getElementById("left");
	oBtn.onclick=function()
	{
			ajax("http://28967904.jsp.jspee.cn/ext/singlePage/list/json-1-1-20",function(str){
				var da= JSON.parse(str);     //JSON資料解析
				alert(da.totalRow)
				},function(erorr){
					console.log('請求出錯:'+erorr);
					})
	}
	}

</script>
</body>
</html>