AJAX权限被拒绝在IE浏览器?被拒、权限、浏览器、AJAX

由网友(爱走就走@)分享简介:我试图使一个AJAX调用外部域已经允许外部请求通过发送访问控制 - 允许 - 产地:* 头,但我获取权限被拒绝在 xmlhttp.post()行。下面是我的code:VAR XMLHTTP;尝试 {XMLHTTP =新的ActiveXObject(MSXML2.XMLHTTP);}赶上(五){尝试 {XMLHTTP...

我试图使一个AJAX调用外部域已经允许外部请求通过发送访问控制 - 允许 - 产地:* 头,但我获取权限被拒绝在 xmlhttp.post()行。

下面是我的code:

  VAR XMLHTTP;
尝试 {
    XMLHTTP =新的ActiveXObject(MSXML2.XMLHTTP);
}赶上(五){
    尝试 {
        XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);
    }赶上(E){
        XMLHTTP =假;
    }
}

如果(XMLHTTP和放大器;!&安培;!typeof运算XMLHtt prequest =未定义){
    尝试 {
        XMLHTTP =新XMLHtt prequest();
    }赶上(五){
        XMLHTTP =假;
    }
}

xmlhttp.onreadystatechange =功能(){
    如果(xmlhttp.readyState == 4和&安培; xmlhttp.status == 200){
        警报(xmlhttp.responseText);
    }
}

xmlhttp.open(GET,http://www.domain.com,真正的);
xmlhttp.setRequestHeader(内容型,应用程序/ x-WWW的形式urlen codeD');
xmlhttp.send();
 

解决方案

我相信,在IE浏览器,你仍然无法使用 XMLHtt prequest 跨域要求。对于这一点,你需要使用 XDomainRequest 对象。完整的文档是这里。我相信,引入独立的对象的原因是为了让开发者制作了肯定会失败,老的浏览器请求之前做了兼容性测试。

为老IE操碎了心 微软再为Windows 7发布安全更新

I'm trying to make an ajax call to an external domain that is already allowing external requests by sending Access-Control-Allow-Origin:* header, but i get Permission Denied on the xmlhttp.post() line.

Here is my code:

var xmlhttp;
try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
    try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
        xmlhttp = false;
    }
}

if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
        xmlhttp = new XMLHttpRequest();
    } catch (e) {
        xmlhttp = false;
    }
}

xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        alert(xmlhttp.responseText);
    }
}

xmlhttp.open("GET", "http://www.domain.com", true);
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xmlhttp.send();

解决方案

I believe that in IE, you still can't use XMLHttpRequest for cross domain requests. For that, you need to use XDomainRequest object. Full documentation is here. I believe the reason for introducing the separate object was to allow developers to do a compatibility test before making a request that certainly would fail with older browsers.

阅读全文

相关推荐

最新文章