« 在同事电脑里发现的一张暴强的图片让ASP和MS SQL SERVER的组合支持UTF-8编码 »

转载一个AJAXRequest类~

小李的站上引用过来的,嘿嘿……

var obj = new AJAXRequest();
obj.method="get";
obj.url = "test.asp?action=test";
obj.callback = function (xmlhttp) {
  //操作代码
}
obj.send();

---------------------------------------------------

JavaScript代码
  1. function AJAXRequest() {   
  2.  var xmlhttp = false;   
  3.  var self = this;   
  4.  this.method = "";   
  5.  this.url = "";   
  6.  this.content = "";   
  7.  this.callback = function(obj) {return;}   
  8.  //创建XMLHttpRequest   
  9.  if (window.XMLHttpRequest) {   
  10.   xmlhttp = new XMLHttpRequest();   
  11.  } else if (window.ActiveXObject) {   
  12.   try {   
  13.    xmlhttp = new ActiveXObject("msxml2.XMLHTTP");   
  14.   } catch(e) {   
  15.    try {   
  16.     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");   
  17.    } catch (e) {}   
  18.   }   
  19.  }   
  20.  if (!xmlhttp) {   
  21.   alert("XMLHttpRequest初始化失败!");   
  22.   return false;   
  23.  }   
  24.  this.send = function() {   
  25.   if (!this.method || !this.url) {return false;}   
  26.   xmlhttp.open(this.method, this.url, true);   
  27.   if (this.method == "post") {   
  28.    xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");   
  29.   }   
  30.   xmlhttp.onreadystatechange = function () {   
  31.    if (xmlhttp.readyState == 4) {   
  32.     if (xmlhttp.status == 200) {   
  33.      self.callback(xmlhttp);   
  34.     } else {   
  35.      alert(xmlhttp.status);   
  36.     }   
  37.    }   
  38.   }   
  39.   if (this.method == "post") {   
  40.    xmlhttp.send(this.content);   
  41.   } else {   
  42.    xmlhttp.send(null);   
  43.   }   
  44.  }   
  45. }   

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

Search

最新评论及回复

最近发表

Powered By Z-Blog 1.8 Spirit Build 80710

Copyright 2008 世界的角落. All Rights Reserved.如有涉及侵犯您权益的情况,请和我联系!