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

和自甴很熟 提交于 2020-01-05 05:00:11

问题


Trying to find a work around for users who have IE 7. Basically in my client-side javascript application the below code makes a httprequest to a server running node.js and I get a succesful connection if the client has IE8 but it's unsuccesful in IE7. Thoughts?

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) {}
}

回答1:


not sure but you need to tweak CreateXmlHttpReq function to handle different types of Microsoft's 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;
}


来源:https://stackoverflow.com/questions/10389136/httprequest-to-node-server-works-in-ie-8-but-not-ie-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!