当前位置: 代码迷 >> Web前端 >> 潜水多时,发点货色
  详细解决方案

潜水多时,发点货色

热度:116   发布时间:2012-09-22 21:54:54.0
潜水多时,发点东西
ajax简单封装:
function AjaxSimpleWrap() {

//-------------------
var ieReqVersions = ['Microsoft.XMLHTTP' ,'MSXML.XMLHTTP',
'Msxml2.XMLHTTP.7.0','Msxml2.XMLHTTP.6.0',
'Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0',
'MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP'];
var reqInfo = null;
var xmlHttpReq = null;
var setReqInfo = null;
//---------util:start--------------
var copyProperty = function(destination,source){
for(property in source){
destination[property]=source[property];
}
return destination
};
//---------util:end----------------
//-----------------action:start---------------------
var createReqObj = function(){
if (window.XMLHttpRequest) {
  xmlHttpReq = new XMLHttpRequest(); 
}else if(window.ActiveXObject){
  for(var i=0; i<ieReqVersions.length; i++) {
    try {
      xmlHttpReq = new ActiveXObject(ieReqVersions[i]);
      if(xmlHttpReq){break;}
    }catch(e){
    alert("there's a error in creating ajax-object");
    }
  }
}
};
var callback = function(){
if (xmlHttpReq.readyState == 4) {
if (xmlHttpReq.status == 200) {
if(reqInfo.connectSuccess) reqInfo.connectSuccess();
}else{
if(reqInfo.connectFail){reqInfo.connectFail();}
else {alert("Not able to retrieve description" + xmlHttpReq.statusText + xmlHttpReq.status);}
}
}
};
//------------------action:end----------------------
//---------------interface:start--------------------
this.setReqInfo = setReqInfo = function(param){
reqInfo=copyProperty({
reqMode:"get",
reqURL:null,
reqParam:null,
connectSuccess:null,
connectFail:null
},param||{});

};
this.connect = function(param){
if(typeof param!='undefined') setReqInfo(param);

var reqmode = reqInfo.reqMode.toLowerCase();
if(reqmode=='get'){
var reqString = reqInfo.reqURL+"?"+reqInfo.reqParam;
xmlHttpReq.open('GET',reqString);
xmlHttpReq.onreadystatechange = callback;
xmlHttpReq.send(null);
}else if(reqmode=='post'){
xmlHttpReq.open('POST', reqInfo.reqURL);
    xmlHttpReq.onreadystatechange = callback;
    xmlHttpReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
    xmlHttpReq.send(reqInfo.reqParam);
}
};
this.getXML = function(){
return xmlHttpReq.responseText;
};
this.getText = function(){
return xmlHttpReq.responseXML;
};
//---------------interface:end--------------------
(function(){
createReqObj();
})();
}

=============================================================
例子:
AjaxObj.connect({reqMode:"get",
reqURL:"wh/servlet/EmailAjaxServlet",
reqParam:("type=2&mailboxid=123&userreqid=123"),
connectSuccess:reciveSuccess,
connectFail:reciveFail});


-------------------------------
其他一坨java的东西还在做,差不多的时候再发出来
  相关解决方案