DIV CSS 佈局教程網

Ajax類
編輯:AJAX詳解     
使用方法
var classajax = new AJax(url,recvT,stringS,resultF);
classAJax.post();//post方式發送數據
classAJax.get();//get方式發送數據
url發送地址 recvT 接受數據類型 0為XML 1為text resultF 返回結果處理函數


var ajax1 = new AJax("1.ASP",0,"id="+id,bacal);
AJax1.post();
function bacal(rexm){
返回數據處理函數;
}


程序代碼

function AJax(url,recvT,stringS,resultF) {
this.url = url;
this.stringS = stringS;
this.xmlHttp = this.createXMLHttpRequest();
if (this.XMLHttp == null) {
alert("erro");
return;
}
var objxml = this.XMLHttp;
objxml.onreadystatechange = function (){AJax.handleStateChange(objXML,recvT,resultF)};
}

AJax.prototype.createXMLHttpRequest = function() {
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
try { return new XMLHttpRequest(); } catch(e) {}
return null;
}

AJax.prototype.createQueryString = function () {
var queryString = this.stringS;
return queryString;
}

AJax.prototype.get = function () {
url = this.url;
var queryString = url+"?timeStamp=" + new Date().getTime() + "&" + this.createQueryString();
this.XMLHttp.open("GET",queryString,true);
this.XMLHttp.send(null);
}

AJax.prototype.post = function() {
url = this.url;
var url = url + "?timeStamp=" + new Date().getTime();
var queryString = this.createQueryString();
this.XMLHttp.open("POST",url,true);
this.XMLHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
this.XMLHttp.send(queryString);
}

AJax.handleStateChange = function (XMLHttp,recvT,resultF) {
if (XMLHttp.readyState == 4) {
if (XMLHttp.status == 200) {
resultF(recvT?xmlHttp.responseXML:XMLHttp.responseText);
} else {
alert("您所請求的頁面有異常。");
}
}
}

 
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved