的HTTPRequest节点服务器工作在IE 8但不是IE 7(HTTPRequest to Nod

2019-10-16 15:39发布

试图找到谁拥有IE 7基本上在我的客户端用户一个变通JavaScript应用下面的代码,使一个HTTPRequest,运行的node.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) {}
}

Answer 1:

不知道,但你需要调整CreateXmlHttpReq函数来处理不同类型的微软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;
}


文章来源: HTTPRequest to Node server works in IE 8 but not IE 7