一段兼容FF,IE6,7,8的AJAX 回調(diào)框架
var ajax = new Object();
ajax.$x = function(url,onload,onerror,stateArray){
this.url = url;
this.req = null;
this.onload = onload;
this.onerror = (onerror) ? onerror : this.defaultError;
this.stateNum = (stateArray) ? stateArray : false;
this.loadXMLDoc(url);
};
ajax.$x.prototype = {
loadXMLDoc : function(url){
if(window.XMLHttpRequest){ //IE 不兼容此種格式的 XHR 申請 這個(gè)是為FF準(zhǔn)備的
this.req = new XMLHttpRequest();
if(this.req.overrideMimeType){
this.req.overrideMimeType('text/xml');
}
}else if(window.ActiveXObject){ //IE 若干類型,具體不知
try{
this.req = new ActiveXObject("Msxml3.XMLHTTP");
}catch(e){
try{
this.req = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
this.req = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){}
}
}
}
if(this.req){
try{
var loader = this;
this.req.onreadystatechange = function(){ // XHR 對象的readystate 有四個(gè)狀態(tài),具體參考(《AJAX IN ACTION》)狀態(tài)的改變會(huì)觸發(fā)該事件,其中標(biāo)記為4的狀態(tài)表示回調(diào)函數(shù)調(diào)用成功,最為關(guān)鍵(個(gè)人認(rèn)為最為關(guān)鍵)
loader.onReadyState.call(loader);
}
this.req.open('GET',url,true);
this.req.send(null);
}catch(err){
this.onerror.call(this);
}
}
},
onReadyState:function(){
var req = this.req;
var ready = req.readyState;
if(this.stateNum && ready >= 1 && ready <= 3){
this.stateNum[ready-1].call(this);
}else if(ready == 4){
var httpStatus = req.status;
}else{
this.onerror.call(this);
}
}
},
defaultError:function(){
alert("數(shù)據(jù)鏈接錯(cuò)誤!"
+ "\n\nreadyStatus: " + this.req.readyStatus
+ "\nstatus: " + this.req.status
+ "\nheafer: " + this.req.getAllResponseHeaders()
)
}
};
function callback(){
var res = this.req.responseText;
ajax.$x = function(url,onload,onerror,stateArray){
this.url = url;
this.req = null;
this.onload = onload;
this.onerror = (onerror) ? onerror : this.defaultError;
this.stateNum = (stateArray) ? stateArray : false;
this.loadXMLDoc(url);
};
ajax.$x.prototype = {
loadXMLDoc : function(url){
if(window.XMLHttpRequest){ //IE 不兼容此種格式的 XHR 申請 這個(gè)是為FF準(zhǔn)備的
this.req = new XMLHttpRequest();
if(this.req.overrideMimeType){
this.req.overrideMimeType('text/xml');
}
}else if(window.ActiveXObject){ //IE 若干類型,具體不知
try{
this.req = new ActiveXObject("Msxml3.XMLHTTP");
}catch(e){
try{
this.req = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
this.req = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){}
}
}
}
if(this.req){
try{
var loader = this;
this.req.onreadystatechange = function(){ // XHR 對象的readystate 有四個(gè)狀態(tài),具體參考(《AJAX IN ACTION》)狀態(tài)的改變會(huì)觸發(fā)該事件,其中標(biāo)記為4的狀態(tài)表示回調(diào)函數(shù)調(diào)用成功,最為關(guān)鍵(個(gè)人認(rèn)為最為關(guān)鍵)
loader.onReadyState.call(loader);
}
this.req.open('GET',url,true);
this.req.send(null);
}catch(err){
this.onerror.call(this);
}
}
},
onReadyState:function(){
var req = this.req;
var ready = req.readyState;
if(this.stateNum && ready >= 1 && ready <= 3){
this.stateNum[ready-1].call(this);
}else if(ready == 4){
var httpStatus = req.status;
if(httpStatus == 200 || httpStatus == 0){ // 我們平時(shí)遇到的404狀態(tài)等都是httpStatus,其實(shí)httpStatus 在200 到299之間都可以認(rèn)為是成功狀態(tài)。
從服務(wù)器返回后,應(yīng)該觸發(fā)回調(diào)函數(shù),onload,在我們的例子中,就是 callback 函數(shù)
}else{
this.onerror.call(this);
}
}
},
defaultError:function(){
alert("數(shù)據(jù)鏈接錯(cuò)誤!"
+ "\n\nreadyStatus: " + this.req.readyStatus
+ "\nstatus: " + this.req.status
+ "\nheafer: " + this.req.getAllResponseHeaders()
)
}
};
function callback(){
var res = this.req.responseText;
};
var ajax_get_answer = function(){
var url = "gameAjax.do?state=";
for(var i = 0;i < elements.length;i++){
url = url + elements[i].children[0].value;
}
new ajax.$x(url,callback,null);
};