1、ajax對象的封裝代碼,包含將xml和xsl客戶端合成代碼
function Ajax() {}
Ajax.prototype = {
sendRequest: function(url, callBack, postData, anscr, method, isSetContentType) {
var xmlhttp = GetXmlHttpObject();
xmlhttp.onreadystatechange = callBackHandle;
xmlhttp.open(method, url , anscr);
if(isSetContentType)
xmlhttp.setRequestHeader("CONTENT-TYPE", "application/x-www-form-urlencoded");
xmlhttp.send(postData);
function callBackHandle() {
if (xmlhttp.readyState == 4){
if (xmlhttp.status == 200) {
callBack(0, xmlhttp.responseXML);
xmlhttp = null;
}else{
callBack(1, xmlhttp.responseXML);
xmlhttp = null;
}
}
}
},
XMLXSLTOHTML:function(xmlDoc, xslpath) { //此方法當(dāng)前版本只支技IE瀏覽器
this.sendRequest(xslpath, thisCallBack, null, false, "GET");
this.html = "";
function thisCallBack(code, returnData) {
if(code == 0) {
html = xmlDoc.transformNode(returnData.documentElement);
}else{
alert("");
}
}
return html;
},
show:function() {
alert("test ajax object new ok");
}
};
function GetXmlHttpObject(){
var objXmlHttp = null
if (navigator.userAgent.indexOf("Opera") >= 0){
alert("This example doesnt work in Opera")
return;
}
if (navigator.userAgent.indexOf("MSIE") >= 0){
var strName = "Msxml2.XMLHTTP"
if (navigator.appVersion.indexOf("MSIE 5.5")>=0){
strName = "Microsoft.XMLHTTP"
}
try{
objXmlHttp = new ActiveXObject(strName)
//objXmlHttp.onreadystatechange=handler
return objXmlHttp
}
catch(e){
alert("Error. Scripting for ActiveX might be disabled")
return
}
}
if (navigator.userAgent.indexOf("Mozilla") >= 0){
objXmlHttp = new XMLHttpRequest()
//objXmlHttp.onload=handler
//objXmlHttp.onerror=handler
return objXmlHttp
}
}
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點(diǎn)擊舉報(bào)。