当 ajax 目标是 localhost 时,IE 10 和 11 中的访问被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22098259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 10:24:11  来源:igfitidea点击:

Access denied in IE 10 and 11 when ajax target is localhost

ajaxxmlhttprequestcorsinternet-explorer-10internet-explorer-11

提问by narc88

I'm trying to do a ajax call between a server (http) that is on internet. And target that to my own localhost. FF/Chrome/ ETC... works. It's ONLY an IE issue. IM USING IE 11 AND 10.

我正在尝试在 Internet 上的服务器 (http) 之间进行 ajax 调用。并将其定位到我自己的本地主机。FF/Chrome/ ETC ... 有效。这只是一个IE问题。我使用 IE 11 和 10。

The request is don't even done. The "denied access" is thrown instantly.

请求甚至没有完成。立即抛出“拒绝访问”。

This is the code. Just for you to see.

这是代码。只为给你看。

Is not the classical HTTP/HTTPS error in IE8 AND IE9. This is something else, but the documentation is not helpful.

不是 IE8 和 IE9 中的经典 HTTP/HTTPS 错误。这是另一回事,但文档没有帮助。

$jq.ajax({
            contentType: 'application/json',
            url: url,
            dataType: 'json',
            crossDomain: true,
            beforeSend: function (xhr) {
                xhr.withCredentials = true; 
                xhr.setRequestHeader("Authorization", "Basic " + $jq.base64.encode(username and password));
            },
            success: function (data, status, headers) {},
            error: function (xhr, status, error) {}

The status is 0in xhrobject and error is "Denied access"

状态0xhr对象中,错误是“拒绝访问”

回答by oobug

Internet Explorer raises this error as part of its security zones feature. Using default security settings, an "Access is Denied" error is raised when attempting to access a resource in the "Local intranet" zone from an origin in the "Internet" zone.

作为其安全区域功能的一部分,Internet Explorer 会引发此错误。使用默认安全设置,尝试从“Internet”区域中的源访问“本地 Intranet”区域中的资源时,会引发“访问被拒绝”错误。

If you were writing your Ajax code manually, Internet Explorer would raise an error when you try to open the resource. For example:

如果您手动编写 Ajax 代码,当您尝试打开资源时 Internet Explorer 会引发错误。例如:

var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://localhost/', true); // This line will trigger an error
xhr.send();

You can work around this error by adding the origin site to the "Trusted sites" security zone. You can test this by adding "http://client.cors-api.appspot.com" to your "Trusted sites" zone and using this test page at test-cors.orgwith your localhost site as the Remote URL.

您可以通过将源站点添加到“受信任的站点”安全区域来解决此错误。您可以通过将“http://client.cors-api.appspot.com”添加到“受信任的站点”区域并使用test-cors.org上的此测试页面并将您的本地主机站点作为远程 URL来测试这一点。

回答by Martin Laukkanen

In addition to the trusted site requirement I found that the problem was not fixed until I used the same protocol for the request as my origin, e.g. my test site was hosted on a https but failed with any destination using http (without the s).

除了受信任的站点要求之外,我发现问题没有得到解决,直到我对请求使用与我的源相同的协议,例如,我的测试站点托管在 https 上,但使用 http(没有 s)的任何目的地都失败了。

This only applies to IE, Chrome just politely logs a warning in the debug console and doesn't fail.

这仅适用于 IE,Chrome 只是礼貌地在调试控制台中记录警告并且不会失败。

回答by Ray Nicholus

If you are attempting to make cross-origin ajax requests in IE9, you'll need to use XDomainRequestinstead of XMLHttpRequest. There is a jQuery plug-in that wraps XDR. You should be aware that there are some notable limitations of XDR.

如果您正试图使IE9跨域Ajax请求,您需要使用XDomainRequest代替XMLHttpRequest。有一个包含 XDR 的 jQuery 插件。您应该知道 XDR 有一些明显的限制。

Another option would be to use a library like this: https://github.com/jpillora/xdomain.

另一种选择是使用这样的库:https: //github.com/jpillora/xdomain

回答by Jamie Holdstock

jQuery implements ajax calls using the XMLHttpRequestobject which is not supported in IE9. You have to force it to use XDomainRequestinstead.

jQuery 使用XMLHttpRequestIE9 不支持的对象实现 ajax 调用。您必须强制使用它XDomainRequest

I get around this problem using this jQuery plugin:

我使用这个 jQuery 插件解决了这个问题:

https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest

https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest

回答by ali bagheri

Note:

笔记:

Do not use "http://www.domain.xxx" or "http://localhost/" or "IP" for URL in Ajax. Only use path(directory) and page name without address.

不要在 Ajax 中使用“ http://www.domain.xxx”或“ http://localhost/”或“IP”作为 URL。只使用路径(目录)和页面名称而不使用地址。

false state:

错误状态:

var AJAXobj = createAjax();
AJAXobj.onreadystatechange = handlesAJAXcheck;
AJAXobj.open('POST', 'http://www.example.com/dir/getSecurityCode.php', true);
AJAXobj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
AJAXobj.send(pack);

true state:

真实状态:

var AJAXobj = createAjax();
AJAXobj.onreadystatechange = handlesAJAXcheck;
AJAXobj.open('POST', 'dir/getSecurityCode.php', true);   // <<--- note
AJAXobj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
AJAXobj.send(pack);



function createAjax()
{
    var ajaxHttp = null;
    try
    {
        if(typeof ActiveXObject == 'function')
            ajaxHttp = new ActiveXObject("Microsoft.XMLHTTP");
        else 
        if(window.XMLHttpRequest)
            ajaxHttp = new XMLHttpRequest();
    }
    catch(e)
    {
        alert(e.message);
        return null;
    }
    //-------------
    return ajaxHttp;
};