IE 8安全设置禁止javascript发送xmlHTTPRequest

IE 8 security settings prevents javascript to send xmlHTTPRequest

本文关键字:javascript 发送 xmlHTTPRequest 禁止 设置 安全 IE      更新时间:2023-09-26

我面临的问题是,每当我在IE8中关闭安全设置时,在我的应用程序中,xmlHTTPRequest调用失败。然而,当我打开它,它的工作很好。安全特性是:工具-> Internet选项->高级->安全->启用原生xmlHTTP支持。如果检查了,则不会发生问题,但如果没有检查,则xmlHTTPReq将无法向服务器发送请求。(我在调试器窗口中没有看到cgi调用)。

所以我的问题是:是否有可能检测到这个安全设置是启用的还是不使用JavaScript编程?

我的cgi调用代码如下:

try{
        var xmlHttpReq;
        var destinationURL = cgiBinPath+"helloworld.cgi?start";
        // IE 7+, Mozilla/Safari
        if (window.XMLHttpRequest) {
            xmlHttpReq=new XMLHttpRequest();
        }
        //IE 5,6
        else {
            xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (xmlHttpReq.readyState == 0){
            xmlHttpReq.open('GET', destinationURL, true);
            xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xmlHttpReq.onreadystatechange = function(){
                if(xmlHttpReq.readyState == 4){
                  //Control never reaches here
                }
            }               
            xmlHttpReq.send();
        }
    }catch(e){
        alert('Exception '+e);
    }  

看一看这篇关于MSDN上原生XMLHTTP的文章。它还为您的问题提供了解决方案。

编辑对不起,我没有仔细阅读这篇文章。这个会回答你的问题。您必须尝试是否可以创建XMLHttpRequest对象的实例。

if (window.XMLHttpRequest) {
    try {
        xmlHttpReq = new XMLHttpRequest();
    } catch (ex) {
        xmlHttpReq = new window.ActiveXObject("Microsoft.XMLHTTP");
    }
} else {
    //...
}