javascript - Node 服务器的 HTTPRequest 在 IE 8 中工作,但在 IE 7 中不起作用

标签 javascript node.js httprequest

尝试为使用 IE 7 的用户找到解决方法。基本上,在我的客户端 javascript 应用程序中,下面的代码向运行 node.js 的服务器发出 httprequest,如果客户端使用 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;
}

关于javascript - Node 服务器的 HTTPRequest 在 IE 8 中工作,但在 IE 7 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10389136/

相关文章:

javascript - Promise 未定义而不是 bool 值

node.js - 在 Azure 移动服务 NodeJS 中使用 google.maps

javascript - mysql查询时异步 waterfall 不遵循顺序

c# - HttpWebRequest WebException 协议(protocol)错误

javascript - 如何在饼图中添加和操作值?

javascript - 同时设置多个元素的样式

node.js - Socket.io 发出回调不在服务器端触发?

c++ - 使用 Qt 的 RESTful API 请求

grails - 有没有办法避免放大器?要求在GrailsParameterMap中请求参数名称

javascript - Google map API - 以客户当前位置为中心的 map