最新消息:发现真没时间折腾VPS,最近又换了个空间。呵呵

Ajax的使用

最近在做一个项目,不用JavaScript框架,直接写Ajax代码,在网上找了一下,找到了如下一段代码。

JavaScript
var Ajax = function(option) {
     var request;
     var CreateRquest = function() {
         var httpRequest;
         try {
             httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
         } catch(e) {
             try {
                 httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
             } catch(e1) {
                 httpRequest = new XMLHttpRequest();
             }
         }
         return httpRequest;
     };
     var SendRequest = function() {
         request = CreateRquest();
         request.open("post", option.Url, true);
         request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

         request.onreadystatechange = ResponseRequest;
         request.send(option.Param);
     };
     var ResponseRequest = function() {
         if (request.readystate == 4) {
             if (request.status == 200) {
                 option.Success(request);
             } else {
                 option.Failure(request);
             }
         }
     };
     SendRequest();
};
var OnBlur = function (el) {
     var option = {
         Url : "test.php",
         Param : "text=abv&id=1",
         Success : function(request) {
             alert(request.responseText);
         },
         Failure : function(request) {
             alert(false);
         }
     };
     new Ajax(option);
};

参考资料:http://tech.ddvip.com/2008-11/122794066396155.html

转载请注明:宇托的狗窝 » Ajax的使用

发表我的评论
取消评论

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址