復(fù)制代碼 代碼如下:
// AJAX類
function AJAXRequest() {
var xmlObj = false;
var CBfunc,ObjSelf;
ObjSelf=this;
try { xmlObj=new XMLHttpRequest; }
catch(e) {
try { xmlObj=new ActiveXObject("MSXML2.XMLHTTP"); }
catch(e2) {
try { xmlObj=new ActiveXObject("Microsoft.XMLHTTP"); }
catch(e3) { xmlObj=false; }
}
}
if (!xmlObj) return false;
this.method="POST";
this.url;
this.async=true;
this.content="";
this.callback=function(cbobj) {return;}
this.send=function() {
if(!this.method||!this.url||!this.async) return false;
xmlObj.open (this.method, this.url, this.async);
if(this.method=="POST") xmlObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlObj.onreadystatechange=function() {
if(xmlObj.readyState==4) {
if(xmlObj.status==200) {
ObjSelf.callback(xmlObj);
}
}
}
if(this.method=="POST") xmlObj.send(this.content);
else xmlObj.send(null);
}
}
AJAX請求類
by HotHeart(熱血心腸)
Site: http://www.xujiwei.cn/
Blog: http://www.xujiwei.cn/blog/
類名:AJAX
創(chuàng)建方法:var ajaxobj=new AJAX;,如果創(chuàng)建失敗則返回false
屬性:method - 請求方法,字符串,POST或者GET,默認(rèn)為POST
url - 請求URL,字符串,默認(rèn)為空
async - 是否異步,true為異步,false為同步,默認(rèn)為true
content - 請求的內(nèi)容,如果請求方法為POST需要設(shè)定此屬性,默認(rèn)為空
callback - 回調(diào)函數(shù),即返回響應(yīng)內(nèi)容時(shí)調(diào)用的函數(shù),默認(rèn)為直接返回,回調(diào)函數(shù)有一個(gè)參數(shù)為XMLHttpRequest對象,即定義回調(diào)函數(shù)時(shí)要這樣:function mycallback(xmlobj)
方法:send() - 發(fā)送請求,無參數(shù)
一個(gè)例子:
script type="text/javascript" src="ajaxrequest.js">/script>
script type="text/javascript">
var ajaxobj=new AJAXRequest; // 創(chuàng)建AJAX對象
ajaxobj.method="GET"; // 設(shè)置請求方式為GET
ajaxobj.url="default.asp" // URL為default.asp
// 設(shè)置回調(diào)函數(shù),輸出響應(yīng)內(nèi)容
ajaxobj.callback=function(xmlobj) {
document.write(xmlobj.responseText);
}
ajaxobj.send(); // 發(fā)送請求
/script>
您可能感興趣的文章:- 如何解決Ajax請求結(jié)果的緩存問題說明
- jquery ajax請求實(shí)例深入解析
- jQuery Ajax請求狀態(tài)管理器打包
- Ajax請求中的異步與同步,需要注意的地方說明
- 多ajax請求的各類解決方案(同步, 隊(duì)列, cancel請求)
- Jquery Ajax請求代碼(2)
- jquery的ajax請求全面了解