zoukankan      html  css  js  c++  java
  • 封装XMLHttpReuqest

    //xmlHttp.js
    var Request = new Object();
    Request.reqList = [];
    function createXMLRequest()
    {
        var xmlHttp=false;
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e){
          try {
              xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
          }
         catch (E){
              xmlHttp = false;
          }
        }
        if (!xmlHttp && typeof XMLHttpRequest!='undefined')
        {
            xmlHttp = new XMLHttpRequest();
        }
        return xmlHttp;
    }

    Request.send = function(url, method, callback, data)
     {
        var xmlHttp=createXMLRequest();
        xmlHttp.onreadystatechange = function()
        {
        if (xmlHttp.readyState == 4)
        {
                if (xmlHttp.status < 400)
                {
                    callback(xmlHttp,data);
                }
                else
                {
                    alert("当加载数据时发生错误 :\n" + xmlHttp.status+ "/" + xmlHttp.statusText);
                }
                //删除XMLHTTP,释放资源
                try {
                    delete xmlHttp;
                    xmlHttp = null;
                } catch (e) {}
            }
        }
        //如果以POST方式回发服务器
        if (method=="POST")
         {
            xmlHttp.open("POST", url, true);
            xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');        
            xmlHttp.send(data);
            Request.reqList.push(xmlHttp);
        }
        //以GET方式请求
        else
        {
            xmlHttp.open("GET", url, true);
            xmlHttp.send(null);
            Request.reqList.push(xmlHttp);
        }
        return xmlHttp;
    }
    //全部清除XMLHTTP数组元素,释放资源
    Request.clearReqList = function()
    {
        var ln = Request.reqList.length;
        for(var i=0; i<ln; i++)
        {
            var xmlHttp = Request.reqList[i];
            if(xmlHttp)
            {
                try{
                    delete xmlHttp;
                } catch (e) {}
            }
        }
        Request.reqList = [];   
    }

    //进一步封装XMLHTTP以GET方式发送请求时的代码
    Request.sendGET = function(url,callback)
    {
        Request.clearReqList();
        return Request.send(url, "GET", callback, null);
    }
    //进一步封装XMLHTTP以POST方式发送请求时的代码
    Request.sendPOST = function(url, callback,data)
    {
        Request.clearReqList();
        Request.send(url, "POST", callback, data);
    }

    HelloWorld 示例:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>无标题页</title>
        <script type="text/javascript" src="xmlHttp.js"></script>
        <script type="text/javascript">
        function callback(req,data)
        {
            document.getElementById("msg").innerHTML = req.responseText;
        }
        </script>
    </head>
    <body>
        <input id="Button1" type="button" value="button" onclick="Request.sendGET('HelloWorld.txt', callback)" />
        <div id="msg"></div>
    </body>
    </html>

    HelloWorld.txt
    就一行:HelloWorld


  • 相关阅读:
    SharePoint 2013 商务智能报表发布
    sharepoint designer web 服务器似乎没有安装microsoft sharepoint foundation
    SharePoint 2013 Designer系列之数据视图
    SharePoint 2013 Designer系列之数据视图筛选
    SharePoint 2013 Designer系列之自定义列表表单
    SharePoint 2013 入门教程之创建及修改母版页
    SharePoint 2013 入门教程之创建页面布局及页面
    SharePoint 2010 级联下拉列表 (Cascading DropDownList)
    使用SharePoint Designer定制开发专家库系统实例!
    PL/SQL Developer 建立远程连接数据库的配置 和安装包+汉化包+注册机
  • 原文地址:https://www.cnblogs.com/LinFx/p/2123731.html
Copyright © 2011-2022 走看看