XmlHtml.js (不会使用这个啊,)怎么办啊,有谁会使用吗?帮个忙啊?

本文介绍了一个用于创建XMLHttpRequest对象的方法,该对象支持异步状态事件处理,并提供了初始化、设置请求头、发送GET/POST请求等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

function CreateXmlHttpRequest(funNameHandler){
//funNameHandler 为异步状态事件处理函数
// init XmlHttpRequest

//xmlhttp和xmldom对像
this.xhttp = null;
this.xdom = null;

//post或get发送数据的键值对
this.keys = Array();
this.values = Array();
this.keyCount = -1;

//http请求头

this.rkeys = Array();
this.rvalues = Array();
this.rkeyCount = -1;
//请求头类型

this.rtype = 'text';

//初始化 xmlhttpRequest 对象
this.xhttp = CreateXmlHttpRe(funNameHandler);

//以下为成员函数

//--------------------------------

//初始化xmldom
this.InitXDom = function(){
  var obj = null;
  if (typeof(DOMParser) != "undefined") { // Gecko、Mozilla、Firefox
    var parser = new DOMParser();
    obj = parser.parseFromString (xmlText, "text/xml");
  } else { // IE
    try { obj = new ActiveXObject("MSXML2.DOMDocument");} catch (e) { }
    if (obj == null) try { obj = new ActiveXObject("Microsoft.XMLDOM"); } catch (e) { }
  }
  this.xdom = obj;
};

//增加一个POST或GET键值对
this.AddKey = function(skey,svalue){
 this.keyCount++;
 this.keys[this.keyCount] = skey;
 this.values[this.keyCount] = escape(svalue);
};

//增加一个Http请求头键值对
this.AddHead = function(skey,svalue){
 this.rkeyCount++;
 this.rkeys[this.rkeyCount] = skey;
 this.rvalues[this.rkeyCount] = svalue;
};

//清除当前对象的哈希表参数
this.ClearSet = function(){
 this.keyCount = -1;
 this.keys = Array();
 this.values = Array();
 this.rkeyCount = -1;
 this.rkeys = Array();
 this.rvalues = Array();
};

//发送http请求头

this.SendHead = function(){
 if(this.rkeyCount!=-1){ //发送用户自行设定的请求头

   for(;i<=this.rkeyCount;i++){
    this.xhttp.setRequestHeader(this.rkeys[i],this.rvalues[i]);
   }
  }
 if(this.rtype=='binary'){
   this.xhttp.setRequestHeader("Content-Type","multipart/form-data");
  }else{
   this.xhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  }
};

//用Post方式发送数据

this.SendPost = function(purl){
 var pdata = "";
 var i=0;
 this.state = 0;
 this.xhttp.open("POST", purl, true);
 this.SendHead();
  if(this.keyCount!=-1 ){ //post数据
   for(;i<=this.keyCount;i++){
    if(pdata=="") pdata = this.keys[i]+'='+this.values[i];
    else pdata += "&"+this.keys[i]+'='+this.values[i];
   }
  }
  this.xhttp.send (pdata);
};

//用GET方式发送数据

this.SendGet = function(purl){
 var gkey = "";
 var i=0;
 this.state = 0;
 if(this.keyCount!=-1){ //get参数
   for(;i<=this.keyCount;i++){
    if(gkey=="") gkey = this.keys[i]+'='+this.values[i];
    else gkey += "&"+this.keys[i]+'='+this.values[i];
   }
   if(purl.indexOf('?')==-1) purl = purl + '?' + gkey;
   else  purl = purl + '&' + gkey;
  }
  this.xhttp.open("GET",purl,true);
  this.SendHead();
  this.xhttp.send();
};

} // End Class DedeAjax

// Create XmlHttpRequest Object
function CreateXmlHttpRe(handler) {
 var agt = navigator.userAgent.toLowerCase();
 var is_op = (agt.indexOf("opera") != -1);
 var is_ie = (agt.indexOf("msie") != -1) && document.all && !is_op;
 var is_ie5 = (agt.indexOf("msie 5") != -1) && document.all && !is_op;
 var thexmlhttp = null;  if (is_ie) {    // Guaranteed to be ie5 or ie6
    var control = (is_ie5) ? "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP";
    try {
     thexmlhttp = new ActiveXObject(control);
     thexmlhttp.onreadystatechange = handler;
    } catch (ex) {
      // TODO: better help message
      alert("You need to enable active scripting and activeX controls"); 
   }
  } else {
   // Mozilla,firefox
    thexmlhttp = new XMLHttpRequest();
    thexmlhttp.onload = handler;
    thexmlhttp.onerror = handler;
 }
 return thexmlhttp;
};
// Get document Elemnet
function getElement(name) {
    if (document.all) {
      return document.all(name);
    }
    return document.getElementById(name);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

rjzou2006

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值