//创建ajax通信对象 function AjaxComm() { //properties declaration /******/ this.xmlHttp = null; this.respFunc = null; this.iSoapMaxLen = 480; this.strUrl = ""; /******/ AjaxComm.prototype.CommObject = this; /**************/ //保留此创建函数,兼容传统的js文件引入形式 AjaxComm.prototype.create__xmlHttp = function() { if((typeof(__gcomm__xmlHttp) != "undefined") && __gcomm__xmlHttp) { this.xmlHttp = __gcomm__xmlHttp; return; } ///////// //Init XMLHttpRequest Object; if(window.ActiveXObject) { //IE Browser var MSXML = ['Microsoft.XMLHTTP', 'Msxml2.XMLHTTP', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP.4.0', 'Msxml2.XMLHTTP.5.0']; for(var i = 0; i < MSXML.length; i++) { try { __gcomm__xmlHttp = new ActiveXObject(MSXML[i]); break; } catch(e) { __gcomm__xmlHttp = null; } } } else if(window.XMLHttpRequest) { //mozilla Browser __gcomm__xmlHttp = new XMLHttpRequest(); //有些版本的Mozilla浏览器处理服务器返回的未包含XML mime-type头部信息的内容时会出错。 if(__gcomm__xmlHttp.overrideMimeType) { __gcomm__xmlHttp.overrideMimeType="text/xml"; } } }; this.create__xmlHttp(); /**************/ ///////// //define CallBack response Function this.ProcResponse = function() { if(!AjaxComm.prototype.CommObject) return; var xmlHttp = AjaxComm.prototype.CommObject.xmlHttp; if(!xmlHttp) return; //////////// //xmlHttp.responseXml, xmlHttp.responseText //xmlHttp.readyState, xmlHttp.statusText //////////// var bGetOk = false; if(xmlHttp.readyState == 4) { if(xmlHttp.status == 200) bGetOk = true; var respFunc = AjaxComm.prototype.CommObject.respFunc; if(respFunc) respFunc(xmlHttp, bGetOk); } }; ///////// ///////// //用于设置缺省的业务处理servlet地址 this.setHostPortUrl = function(strServName, strHost, strPort) { if(!strHost) strHost = window.location.hostname; if(!strPort) strPort = window.location.port; if(!strPort) strPort = 80; this.strUrl = "http://" + strHost + ":" + strPort + "/jwFrame/servlet/" + strServName; }; ///////// this.SendCmd = function(headParam, strMethod, strCmd, respFunc, passport, password, bUrlSeted) { var bAsync = false; if(!this.xmlHttp) this.create__xmlHttp(); if(!bUrlSeted) this.setHostPortUrl("owbservlet"); if(!this.xmlHttp || !this.strUrl) { alert("通信对象未建立或者通信目标地址为空,执行失败 !"); return false; } ////// if(respFunc) { this.respFunc = respFunc; AjaxComm.prototype.CommObject = this; this.xmlHttp.onreadystatechange = this.ProcResponse; bAsync = true; } else { this.xmlHttp.onreadystatechange = function(xmlHttp, bRet){}; this.respFunc = null; } ////// if(!strMethod) strMethod = "POST"; this.xmlHttp.open(strMethod, this.strUrl, bAsync, passport, password); this.xmlHttp.setRequestHeader("Content-Type", "text/html; charset=UTF-8"); var iCmdLength = strCmd ? strCmd.length : 0; this.xmlHttp.setRequestHeader("Content-Leng", NumberToStr(iCmdLength)); //////// if(headParam) { for(var prop in headParam) { var strValue = headParam[prop]; if(!strValue) strValue = ""; this.xmlHttp.setRequestHeader(prop, strValue); } } //////// var bRet = true; try { this.xmlHttp.send(strCmd); } catch(e) { bRet = false; } return bRet; }; this.getSendPackNo = function() { if(!window.top.__iSendPackNo) { window.top.__iSendPackNo = 1; } else { window.top.__iSendPackNo ++; if(window.top.__iSendPackNo > 40960) window.top.__iSendPackNo = 1; } return window.top.__iSendPackNo; }; ///////// //补丁:发送Execute命令包过长时的解决办法 this.Send_ExecutCmd = function(headParam, strMethod, strCmd, respFunc, passport, password) { if(strCmd && (strCmd.length > this.iSoapMaxLen)) { /////////////// //按长度分段传送命令传 var bRet = false; var iSort = 0; var strCmdFrage = ""; var iSendPackNo = this.getSendPackNo(); var strSendPackFlag = Math.floor(Math.random() * 100000000) + ""; while(true) { strCmdFrage = strCmd.substr(iSort * this.iSoapMaxLen, this.iSoapMaxLen); //格式:PackNo.SortNo.PackCount iSort = iSort + 1; headParam.packno = NumberToStr(iSendPackNo) + "." + NumberToStr(iSort); headParam.sendpackflag = strSendPackFlag; var bSendOver = false; //发完最后一个单元,或者没有新的单元可加 if(iSort * this.iSoapMaxLen >= strCmd.length) { //最后一帧,加上帧长(告之服务器) headParam.packno = headParam.packno + "." + NumberToStr(iSort); bSendOver = true; } var bContinue = false; bRet = this.SendCmd(headParam, strMethod, strCmdFrage, respFunc, passport, password); if(bRet) { if(this.xmlHttp.readyState == 4) { if(this.xmlHttp.status == 200) { bContinue = (this.xmlHttp.responseText == "CONTINUE"); } } } if(!bRet || !bContinue || bSendOver) break; } return bRet; } else { return this.SendCmd(headParam, strMethod, strCmd, respFunc, passport, password); } }; ///////// this.getClientConnId = function() { if(!window.top.iConnectionId || (window.top.iConnectionId <= 0)) { var headParam = {cmdCode:"getcltconnid"}; var strId = this.pubGetServerInfo(headParam); window.top.iConnectionId = StrToInteger(strId, 0); } return window.top.iConnectionId; }; this.Execute = function(strCmd, headParam) { var iCltConId = this.getClientConnId(); if(!headParam) headParam = {}; headParam.cmdCode = "execute"; headParam.conId = iCltConId; var bSendRet = this.Send_ExecutCmd(headParam, "POST", strCmd); if(bSendRet && (this.xmlHttp.readyState == 4)) { if(this.xmlHttp.status == 200) { return this.xmlHttp.responseText; } else { return "ERROR-SERVLET-FAIL"; } } else { var strErrCode = this.xmlHttp.statusText; if(!strErrCode) { if(this.xmlHttp.readyState) strErrCode = this.xmlHttp.readyState; if(this.xmlHttp.status) strErrCode = (strErrCode ? (strErrCode + "-") : "") + this.xmlHttp.status; } if(strErrCode) strErrCode = "NULL"; return "ERROR-CODE: " + strErrCode; } return "ERROR-CLIENT-EXECUTE"; }; this.pubGetServerInfo = function(headParam, strCmd) { var strRet = undefined; var bSendRet = this.SendCmd(headParam, "POST", strCmd); if(bSendRet && (this.xmlHttp.readyState == 4)) { if(this.xmlHttp.status == 200) { strRet = this.xmlHttp.responseText; } } return strRet; }; this.loadServerContent = function(sFileUrl) { var bLoadSucceed = true; if(!this.xmlHttp) this.create__xmlHttp(); if(!this.xmlHttp) { alert("因通信对象未能建立成功,加载失败 !"); return null; } var strSource = ""; try { this.xmlHttp.open('GET', sFileUrl, false); this.xmlHttp.send(null); } catch(e) { bLoadSucceed = false; alert("错误:从服务器加载文件[" + sFileUrl + "]失败!"); return null } if((this.xmlHttp.readyState == 4) && bLoadSucceed) { if(this.xmlHttp.status == 200) { strSource = this.xmlHttp.responseText; } } return strSource; }; this.loadServerFile = function(sFileUrl) { var strSource = this.loadServerContent(sFileUrl); if(strSource) return this.mkJsFileVailable(null, strSource); return false; }; this.mkJsFileVailable = function(strFileId, strSource, winObj, lng) { var bRet = false; if(!winObj) winObj = window; var oHead = winObj.document.getElementsByTagName('HEAD').item(0); if(!oHead) return false; if(!lng) lng = "javascript"; var oScript = winObj.document.createElement("script"); oScript.type = "text/" + lng; oScript.language = lng; if(strFileId) oScript.id = strFileId; oScript.defer = true; oScript.text = strSource; try { oHead.appendChild(oScript); bRet = true; } catch(e) { oHead.removeChild(oScript); } return bRet; }; } /**************/ //释放ajax通信对象 function delAjaxComm(ajaxCommObj) { if(!ajaxCommObj) return; if(ajaxCommObj.xmlHttp) { delete ajaxCommObj.xmlHttp; } delete ajaxCommObj; } /**************/