zoukankan      html  css  js  c++  java
  • 通过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);
                    }
                }

            }

  • 相关阅读:
    THUWC 2019 第二轮 纯口胡题解
    Codeforces Round #607 (Div. 1) Solution
    Codeforces Round #606 (Div. 1) Solution
    CSP-S 2019 简要题解
    NOIP 2018 简要题解
    luogu P5605 小 A 与两位神仙
    luogu P5606 小 K 与毕业旅行
    AtCoder Grand Contest 040 简要题解
    AtCoder Grand Contest 035 简要题解
    AtCoder Grand Contest 036 简要题解
  • 原文地址:https://www.cnblogs.com/lip0121/p/3130904.html
Copyright © 2011-2022 走看看