HTTPRequest to Node 服务器适用于 IE 8,但不适用于 IE 7

HTTPRequest to Node server works in IE 8 but not IE 7

本文关键字:适用于 IE 不适用 服务器 to HTTPRequest Node      更新时间:2023-09-26

试图为拥有IE 7的用户找到解决方法。 基本上在我的客户端 javascript 应用程序中,下面的代码向运行节点的服务器发出 httprequest.js如果客户端有 IE8,我会得到一个成功的连接,但它在 IE7 中不成功。 思潮?

var myxmlhttp;
doRequest();
function doRequest() {
    var url = "http://someserver:8000/" + username;
    myxmlhttp = CreateXmlHttpReq(resultHandler);
    if (myxmlhttp) {
        XmlHttpGET(myxmlhttp, url);
    } else {
        alert("An error occured while attempting to process your request.");
        // provide an alternative here that does not use XMLHttpRequest
    }
}
function resultHandler() {
    // request is 'ready'
    if (myxmlhttp.readyState == 4) {
        // success
        if (myxmlhttp.status == 200) {
            alert("Success!");
            // myxmlhttp.responseText is the content that was received
        } else {
            alert("There was a problem retrieving the data:'n" + req.status.text);
        }
    }
}
function CreateXmlHttpReq(handler) {
    var xmlhttp = null;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        // users with activeX off
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
    }
    if (xmlhttp) xmlhttp.onreadystatechange = handler;
    return xmlhttp;
}
// XMLHttp send GEt request
function XmlHttpGET(xmlhttp, url) {
    try {
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    } catch (e) {}
}
不确定,

但您需要调整CreateXmlHttpReq函数来处理不同类型的Microsoft的ActiveXObjects

function CreateXmlHttpReq(handler) {
    var xmlhttp = null;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        var types = ["Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.3.0", "Microsoft.XMLHTTP"];
        for (var i = 0; i < types.length; i++) {
            try {
                xmlhttp = new ActiveXObject(types[i]);
                break;
            } catch(e) {}
        }
    }
    if (xmlhttp) {
         xmlhttp.onreadystatechange = handler;
    }
    return xmlhttp;
}