• 通过js代码创建XMLHTTPRequest对象


    最近在做一个网站,用到ajax,总结了用javascript代码创建XMLHTTPRequest对象,并且向一个一般处理程序发送请求并在客户端处理响应报文的代码,

    代码如下:

     function createXmlHTTP() {
                var xhr = false;
                try {//ie浏览器
                    xhr = new ActiveXObject("Msxml2.XMLHTTP"); //msxml高版本
                }
                catch (e) {
                    try {
                        xhr = new ActiveXObject("Microsoft.XMLHTTP"); //msxml低版本
                    }
                    catch (e2) {
                        xhr = false;
                    }
                }
                if (!xhr && typeof XMLHttpRequest != "undefined") { //非ie浏览器
                    xhr = new XMLHttpRequest();
                }
                return xhr;
            }
            function aja() {
                var xmlHttp = createXmlHTTP();
                xmlHttp.open("GET", "Handler1.ashx?ajax=1", true); //get请求
                xmlHttp.onreadystatechange = Watching;
                xmlHttp.send(null);
                function Watching() {
                    if (xmlHttp.readyState == 4) {//请求状态
                        if (xmlHttp.status == 200) {//服务器返回的状态码
                            var msg = xmlHttp.responseText; //服务器返回的字符串
                            document.getElementById("txt1").value= msg;
                        } else alert("服务器错误!" + ajaxH.status);
                    }
                }

            }

  • 相关阅读:
    Leetcode-Partition List
    Leetcode-Gray Code
    Leetcode-Subsets II
    Leetcode-Reverse Linked List II
    Leetcode-Resotre IP Addresses
    Leetcode-Decode Ways
    Leetcode-Trapping Rain Water
    EVA 4400存储硬盘故障数据恢复方案和数据恢复过程
    HP DL380服务器RAID信息丢失数据恢复方法和数据恢复过程分享
    ESXi5.0误删除虚拟机还有办法恢复吗?答案是可以!
  • 原文地址:https://www.cnblogs.com/lip0121/p/3130904.html
Copyright © 2020-2023  润新知