一個簡單的AJAX請求類
更新時間:2006年12月23日 00:00:00 作者:
在給blog加上無刷新搜索和即時驗證檢測后,又看了下代碼,感覺太過麻煩,就把XMLHttpRequest請求封裝到一個類里面,用起來方便多了,不用記那么多代碼,什么創(chuàng)建XMLHttpRequest對象什么的,這部分代碼也是重用性比較高的~已經(jīng)打包,在日志的末尾下載。
要看效果的話點開側(cè)邊欄里的日志搜索,里面有一個無刷新搜索,就是了,或者在閱讀日志或留言簿里的注冊碼那里有即時檢測,如果不輸入驗證碼或者輸錯了驗證碼,輸入框都會變紅的^_^
類名:AJAXRequest
創(chuàng)建方法:var ajaxobj=new AJAXRequest;,如果創(chuàng)建失敗則返回false
屬性:method - 請求方法,字符串,POST或者GET,默認為POST
url - 請求URL,字符串,默認為空
async - 是否異步,true為異步,false為同步,默認為true
content - 請求的內(nèi)容,如果請求方法為POST需要設(shè)定此屬性,默認為空
callback - 回調(diào)函數(shù),即返回響應(yīng)內(nèi)容時調(diào)用的函數(shù),默認為直接返回,回調(diào)函數(shù)有一個參數(shù)為XMLHttpRequest對象,即定義回調(diào)函數(shù)時要這樣:function mycallback(xmlobj)
方法:send - 發(fā)送請求,無參數(shù)
一個例子:
<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ā)送請求
// 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);
}
}
要看效果的話點開側(cè)邊欄里的日志搜索,里面有一個無刷新搜索,就是了,或者在閱讀日志或留言簿里的注冊碼那里有即時檢測,如果不輸入驗證碼或者輸錯了驗證碼,輸入框都會變紅的^_^
類名:AJAXRequest
創(chuàng)建方法:var ajaxobj=new AJAXRequest;,如果創(chuàng)建失敗則返回false
屬性:method - 請求方法,字符串,POST或者GET,默認為POST
url - 請求URL,字符串,默認為空
async - 是否異步,true為異步,false為同步,默認為true
content - 請求的內(nèi)容,如果請求方法為POST需要設(shè)定此屬性,默認為空
callback - 回調(diào)函數(shù),即返回響應(yīng)內(nèi)容時調(diào)用的函數(shù),默認為直接返回,回調(diào)函數(shù)有一個參數(shù)為XMLHttpRequest對象,即定義回調(diào)函數(shù)時要這樣:function mycallback(xmlobj)
方法:send - 發(fā)送請求,無參數(shù)
一個例子:
復(fù)制代碼 代碼如下:
<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ā)送請求
復(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);
}
}
相關(guān)文章
ajax方式實現(xiàn)注冊功能(提交數(shù)據(jù)到后臺數(shù)據(jù)庫完成交互)
這篇文章主要介紹了ajax方式實現(xiàn)注冊功能,提交數(shù)據(jù)到后臺數(shù)據(jù)庫完成交互,感興趣的小伙伴們可以參考一下2016-08-08
AJAX入門之深入理解JavaScript中的函數(shù)
AJAX入門之深入理解JavaScript中的函數(shù)...2006-06-06
Jquery基于Ajax方法自定義無刷新提交表單Form實例
這篇文章主要介紹了Jquery基于Ajax方法自定義無刷新提交表單Form的方法,結(jié)合實例詳細分析了Ajax無刷新提交表單的完整實現(xiàn)過程,并總結(jié)了使用中的注意事項,具有很好的借鑒價值,需要的朋友可以參考下2014-11-11

