zoukankan      html  css  js  c++  java
  • Ajax简单

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

    //封装XMLHTTP的Request类的代码
    var Request = new Object();
    //定义一个XMLHTTP的数组
    Request.reqList = [];

    function getAjax()
    {
        var ajax=false;
        try
        {
         ajax = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
         {
          try
          {
          ajax = new ActiveXObject("Microsoft.XMLHTTP");
          }
         catch (E)
          {
          ajax = false;
          }
        }
        if (!ajax && typeof XMLHttpRequest!='undefined')
        {
         ajax = new XMLHttpRequest();
        }

        return ajax;
    }

    Request.send = function(url, method, callback, data, urlencoded, callback2)
     {
        var req=getAjax(); 
        if(!req)
        {
        return false;
        }
     
        req.onreadystatechange = function()
        {
     
        if (req.readyState == 4)
        {
       
       if (req.status ==200)
       {
       
       if(callback)
        callback(req,data);
       }
       
       else
       {
        alert("当加载数据时发生错误 :\n" + req.status+ "/" + req.statusText);
        
        if (callback2)
        callback2(req,data);
       }
       
       afterLoading();
      
       try {
        delete req;
        req = null;
       } catch (e) {}
      }
     }
     
     if (method=="POST")
      {
      req.open("POST", url, true);
      
      if (urlencoded)
          req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');  
      req.send(data);
      Request.reqList.push(req);
     }
     
      else
     {
      req.open("GET", url, true);
      req.send(null);
      Request.reqList.push(req);
     }
     
     loading(); 
     return req;
    }

    Request.clearReqList = function()
    {
     var ln = Request.reqList.length;
     for (var i=0; i<ln; i++) {
      var req = Request.reqList[i];
      if (req)
      {
      try
      {
       delete req;
      } catch(e) {}
      }
     }
     Request.reqList = [];
    }

    Request.sendPOST = function(url, data, callback, clear, callback2)
    {
     if (clear)
      Request.clearReqList();
     Request.send(url, "POST", callback, data, true, callback2);
    }

    Request.sendGET = function(url, callback, args, clear, callback2)
    {
     if (clear)
      Request.clearReqList();
     return Request.send(url, "GET", callback, args, false, callback2);
    }

    function loading()
    {
    var div = $("loadingflag");
    if(div)
    div.style.display ="";
    }

    function afterLoading()
    {
    var div = $("loadingflag");
    if(div)
    div.style.display ="none";
    }

  • 相关阅读:
    Html5结构相关元素
    html5文本元素
    html5全局属性
    元数据元素总结
    千里之行,始于足下
    换个角度思考
    java的权限修饰符
    四则运算
    测量软件使用感受
    JQuery高级
  • 原文地址:https://www.cnblogs.com/luluping/p/1193754.html
Copyright © 2011-2022 走看看