XMLHTTPRequest 的封装--Sender

来源:互联网 发布:vscode golang 跳转 编辑:程序博客网 时间:2024/05/20 23:29
Function.registerNamespace("net.bingosoft.web");
net.bingosoft.web.Sender 
= function(){
 net.bingosoft.web.Sender.initializeBase(
this);
 
this.makeXMLHTTP = function() 
  
var xmlhttp = null;
  
if(window.ActiveXObject) {
   xmlhttp 
= new ActiveXObject("Microsoft.XMLHTTP");
  }
 else if(window.XMLHttpRequest) {
   xmlhttp 
= new XMLHttpRequest();
  }
 else {
   
throw new Error("Could not create xmlhttp on your browser");
  }
 
  
return xmlhttp;
 }

 
 
var _url;
 
var _method;
 
var _async;
 
var _callback;
 
var _sendbody;
 
 
var _xmlhttp   = null;
 
var _headerName   = "Content-Type";
 
var _headerValue  = "text/xml";
 
var _self    = this
 
 
this._onreadystatechange = function() {
  
if(_xmlhttp==nullreturn;
  
if(_xmlhttp.readyState != 4return;
  
if(_xmlhttp.status == 200 || _xmlhttp.status == 304{
   
if(_callback == null{
    
return;
   }
 else if(_callback instanceof Function) {
    
var retXmlDom = _xmlhttp.responseXML;
    
if(retXmlDom.xml==""{
     retXmlDom 
= new ActiveXObject("Microsoft.XMLDOM");
     retXmlDom.loadXML(_xmlhttp.responseText);
    }

    _callback.call(_self,retXmlDom);
   }
 else {
    _callback.call(_self, _xmlhttp.responseXML);
   }

   _xmlhttp 
= null;
  }
 else {
   
if(typeof(processException) != "undefined"{
    processException(_xmlhttp.status,_xmlhttp.statusText);
   }
 else {
    alert(_xmlhttp.status
+""+_xmlhttp.statusText);
   }

  }

 }

 
 
this.send = function(url,method,callback,sendBody) {
  _xmlhttp 
= this.makeXMLHTTP();
 
  _url   
=  url;
  _method  
=  method || "POST";
  _async  
= callback ? true : false;
  _callback 
=  callback || null;
  _sendbody  
=  sendBody || "";
  
  _xmlhttp.open(_method,_url,_async);
  _xmlhttp.setRequestHeader(_headerName,_headerValue);
  
if(_async) {
   _xmlhttp.onreadystatechange 
= function() {
    _self._onreadystatechange.call(_self);
   }

  }

  _xmlhttp.send(_sendbody);
  
if(!_async) {
   
var retXmlDom = _xmlhttp.responseXML;
   
if(retXmlDom.xml==""{
    retXmlDom 
= new ActiveXObject("Microsoft.XMLDOM");
    retXmlDom.loadXML(_xmlhttp.responseText);
   }
 
   
return retXmlDom;
  }

 }

}

net.bingosoft.web.Sender.registerClass(
"net.bingosoft.web.Sender"nullnull);
var $sender = new net.bingosoft.web.Sender();