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

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

问题描述

在任何版本早于 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 库之前添加以下 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!

更新:这似乎与通过不同的子域 (CORS) 访问 API 有关;

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

推荐答案

我在 Google AngularJS 小组的一位经验丰富的 ng-dev 的指导下能够实现的解决方案是这个 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.

设置非常简单,只需在 API 的根目录下放置一个 proxy.html 文件,其中包含允许的来源('master')的正则表达式/字符串,以及对前端脚本的引用,然后从你的前端脚本('master')指向这个文件.

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]-->

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

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