一個(gè)AJAX類代碼
更新時(shí)間:2010年01月17日 21:31:09 作者:
以前寫的一個(gè)AJAX類,兼容性還不錯(cuò),簡(jiǎn)單易用,記下了方便以后找 :)
基本用法:
var ajax = new AjaxObj(url);
ajax.addListener(200, function(r){
alert(r);
});
ajax.send();
也可以連續(xù)調(diào)用:
var ajax = new AjaxObj(url).addListener(200, function(r){
alert(r);
}).send();
另外還支持自定義的POST或GET方式請(qǐng)求,以及監(jiān)視不同的HTTP狀態(tài)碼,自己看代碼琢磨吧 :)
完整代碼:
AjaxObj = function(url, method, content){
this.r = null;
this.url = url;
this.method = method;
this.content = content;
this.header = {};
this.header["Connection"] = "close";
this.header["Content-type"] = "application/x-www-form-urlencoded";
var self = this;
if(window.XMLHttpRequest){
this.r = new XMLHttpRequest();
}else if(window.ActiveXObject){
try {
this.r = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try{
this.r = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
}
}
}
this.addListener = function(http_status, func){
if(!this.L)
this.L=[];
this.L[http_status] = func;
return this;
};
this.setHeader = function(name, value){
this.header[name] = value;
this.r.setRequestHeader(name, value);
return this;
};
this.send = function(){
if(this.method != "post" && this.method != "get")
this.method = "get";
this.r.open(this.method, this.url, true);
for(var h in this.header) {
this.r.setRequestHeader(h, this.header[h]);
}
this.r.send(this.content);
};
if(this.r) this.r.onreadystatechange = function(){
if(self.r.readyState == 4 && self.L[self.r.status] != null)
self.L[self.r.status](self.r.responseText);
};
};
復(fù)制代碼 代碼如下:
var ajax = new AjaxObj(url);
ajax.addListener(200, function(r){
alert(r);
});
ajax.send();
也可以連續(xù)調(diào)用:
復(fù)制代碼 代碼如下:
var ajax = new AjaxObj(url).addListener(200, function(r){
alert(r);
}).send();
另外還支持自定義的POST或GET方式請(qǐng)求,以及監(jiān)視不同的HTTP狀態(tài)碼,自己看代碼琢磨吧 :)
完整代碼:
復(fù)制代碼 代碼如下:
AjaxObj = function(url, method, content){
this.r = null;
this.url = url;
this.method = method;
this.content = content;
this.header = {};
this.header["Connection"] = "close";
this.header["Content-type"] = "application/x-www-form-urlencoded";
var self = this;
if(window.XMLHttpRequest){
this.r = new XMLHttpRequest();
}else if(window.ActiveXObject){
try {
this.r = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try{
this.r = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
}
}
}
this.addListener = function(http_status, func){
if(!this.L)
this.L=[];
this.L[http_status] = func;
return this;
};
this.setHeader = function(name, value){
this.header[name] = value;
this.r.setRequestHeader(name, value);
return this;
};
this.send = function(){
if(this.method != "post" && this.method != "get")
this.method = "get";
this.r.open(this.method, this.url, true);
for(var h in this.header) {
this.r.setRequestHeader(h, this.header[h]);
}
this.r.send(this.content);
};
if(this.r) this.r.onreadystatechange = function(){
if(self.r.readyState == 4 && self.L[self.r.status] != null)
self.L[self.r.status](self.r.responseText);
};
};
相關(guān)文章
Ajax跨域問(wèn)題及解決方案(jsonp,cors)
遠(yuǎn)程服務(wù)器上設(shè)法動(dòng)態(tài)的把數(shù)據(jù)裝進(jìn)js格式的文本代碼段中,供客戶端調(diào)用和進(jìn)一步處理。這篇文章主要介紹了ajax跨域問(wèn)題解決方案(jsonp,cors) ,需要的朋友可以參考下2019-06-06
分享Ajax創(chuàng)建簡(jiǎn)單實(shí)例代碼
這篇文章主要為大家分享了Ajax創(chuàng)建簡(jiǎn)單實(shí)例代碼,學(xué)習(xí)Ajax如何創(chuàng)建簡(jiǎn)單實(shí)例,感興趣的小伙伴們可以參考一下2015-12-12
AJAX+Servlet實(shí)現(xiàn)的數(shù)據(jù)處理顯示功能示例
這篇文章主要介紹了AJAX+Servlet實(shí)現(xiàn)的數(shù)據(jù)處理顯示功能,結(jié)合實(shí)例形式分析了前臺(tái)ajax與后臺(tái)Servlet生成隨機(jī)數(shù)顯示的相關(guān)交互操作技巧,需要的朋友可以參考下2018-06-06
ajax和fetch的區(qū)別點(diǎn)總結(jié)
在本篇文章里小編給大家整理了一篇關(guān)于ajax和fetch的區(qū)別點(diǎn)總結(jié)內(nèi)容,對(duì)此有興趣的朋友們可以跟著學(xué)習(xí)下。2021-12-12
空格或者空白字符導(dǎo)致$.ajax()報(bào)parseerror錯(cuò)誤小結(jié)
這篇文章主要介紹了空格或者空白字符導(dǎo)致$.ajax()報(bào)parseerror錯(cuò)誤,需要的朋友可以參考下2014-04-04
JQuery Ajax動(dòng)態(tài)生成Table表格
實(shí)現(xiàn)過(guò)程是這樣的:前臺(tái)通過(guò)jquery的ajax調(diào)用一般處理程序(Handler),獲取表格需要顯示的信息,然后轉(zhuǎn)換成json格式返回給前臺(tái),前臺(tái)獲取到數(shù)據(jù)后循環(huán)構(gòu)建表格的行,最好把行附加到表里。感興趣的朋友一起看看吧2015-09-09
Ajax傳遞中文參數(shù)到后臺(tái)亂碼的有效解決方法
使用Ajax傳遞中文參數(shù),如果不對(duì)參數(shù)進(jìn)行處理的話,傳到后臺(tái)會(huì)變成亂碼,解決方法很簡(jiǎn)單,需要的朋友可以參考下本文2014-05-05

