XMLHttpRequest访问在任何低于10的IE版本上被AngularJS拒绝 [英] XMLHttpRequest Access Denied with AngularJS on any IE version below 10

查看:163
本文介绍了XMLHttpRequest访问在任何低于10的IE版本上被AngularJS拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在任何版本早于10的Microsoft IE浏览器上查看我的应用程序时,我在控制台上看到以下奇怪的错误:

While viewing my application on any Microsoft IE browser of version earlier than 10, I get the following weird error at the console:

我试图取消控制台,在AngularJS lib之前添加以下JavaScript代码:

I've tried canceling the console with adding the following JavaScript code prior to the AngularJS lib:

console.log = function(){};
window.console = {log: function(){}};

没有什么区别。
同样的错误在IE 10中显示为:

It didn't make a difference. The same error in IE 10 appears as:

SCRIPT7002: XMLHttpRequest: Network Error 0x80070005, Access is denied.

可能我试图从API获取'/ me',以检查用户是否已验证或访客。

probably me trying to get the '/me' from the API in order to check if the user is authenticated or a guest.

基本上。每次服务器给出2XX或3XX以外的响应时,消除那些恼人的控制台错误将是巨大的!

Basically. eliminating those annoying console errors each time the server gives a response other than 2XX or 3XX would be great!

UPDATE:这似乎与访问API在不同的子域(CORS);

UPDATE: This seems to be related to accessing an API over a different sub domain(CORS);

推荐答案

我已经能够通过经验丰富的ng-dev在Google AngularJS群组中是此 xDomain库

The solution that I've been able to achieve via the guidance of an experienced ng-dev at the Google AngularJS group is this xDomain library.

设置是非常简单,只需将一个proxy.html文件放在您的API的根目录,使用一个允许的源('master')的regex /字符串,以及前端脚本的引用,然后指向这个文件

The setup is very simple, simply place a proxy.html file at the root of your API, with a regex/string for an allowed origin('master'), and a reference to the script at the frontend, and then point to this file from your frontend script('master').

它通过在iframe中打开proxy.html文件来工作,并使用 postMessage与CORS资源进行通信

It works by opening the proxy.html file in an iframe, and communicating with the CORS resource using postMessage.

与$ http和$ resource的魅力一样。

Works like a charm with both $http and $resource.

您还可以通过将脚本放在上面的所有JavaScript库包括:

You can also maintain normal functioning for normal browsers by placing the script above all JavaScript library includes:

<!--[if lte IE 9]>
<script src="xdomain.js" slave="http://example.org/proxy.html"></script>
<![endif]-->

这篇关于XMLHttpRequest访问在任何低于10的IE版本上被AngularJS拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆