javascript 在 Angular 和 IE8 中使用 $http 请求拒绝访问

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15960407/
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-10-27 02:45:13  来源:igfitidea点击:

Access Denied with $http request in Angular and IE8

javascriptnode.jsinternet-explorer-8angularjs

提问by cyberwombat

I am trying to get my angular app to do an $http.get request. Works everywhere but IE8. I am using latest Angular (1.0.6 I think). The only message IE gives is:TypeError:

我正在尝试让我的 angular 应用程序执行 $http.get 请求。适用于除 IE8 以外的任何地方。我正在使用最新的 Angular(我认为是 1.0.6)。IE 给出的唯一消息是:TypeError:

Access is denied. undefined (in previous angular this was )

访问被拒绝。未定义(在以前的角度这是 )

My host (nodejs)is set to send cors headers:

我的主机(nodejs)设置为发送 cors 标头:

res.header('Access-Control-Allow-Origin', '*'); //config.allowedDomains
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, X-Requested-By');
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');

I have tried to get more info about the error using debug tools but:

我尝试使用调试工具获取有关错误的更多信息,但是:

Fiddler: doesn't show xmlhttprequests - seems you can hack your NET app to fix that but not sure what to do with my web page. DebugBar: doesn't show these requests either FirebugLite: 2 version I tried are broken - just random errors and doesn't load

Fiddler:不显示 xmlhttprequests - 似乎你可以破解你的 NET 应用程序来解决这个问题,但不确定如何处理我的网页。DebugBar: 不显示这些请求 FirebugLite: 2 版本我试过坏了 - 只是随机错误,不加载

Changing the hosts to same origin clears the issue so I assume it's trying something but man it's hard to debug.

将主机更改为同源可以清除问题,所以我认为它正在尝试某些东西,但是很难调试。

Angular posts on this seem outdated and I am not sure what to do from here.

关于这个的 Angular 帖子似乎已经过时,我不知道从这里开始做什么。

回答by Alan

This has nothing to do with $http. $http lets you make XHR(XDR) or JSONP requests. It doesn't provide a mechanism to circumvent the browsers same origin policy.

这与 $http 无关。$http 允许您发出 XHR(XDR) 或 JSONP 请求。它不提供规避浏览器同源策略的机制。

Changing the hosts to same origin clears the issue so I assume it's trying something but man it's hard to debug.

将主机更改为同源可以清除问题,所以我认为它正在尝试某些东西,但是很难调试。

That's the key to your problem. IE8 doesn't support Cross-Origin-Resource-Sharing (CORS) very well. In order to make cross domain calls with IE8, you better off either:

这是解决您问题的关键。IE8 不能很好地支持跨域资源共享 (CORS)。为了使用 IE8 进行跨域调用,您最好选择:

  1. Use your nodejs app to proxy the request for you, letting you use the same domain.
  2. Use JSONP in your $http.get() request
  1. 使用您的 nodejs 应用程序为您代理请求,让您使用相同的域。
  2. 在您的 $http.get() 请求中使用 JSONP

There are a number of gotchas associated with CORS and IE8, which are outlined here.

有许多与 CORS 和 IE8 相关的问题,在此处进行了概述

回答by Sam Storie

I was running into this same issue, and while it's likely not ideal, one solution is to change IE's 'Access data sources across domains' security setting to Enableor Prompt:

我遇到了同样的问题,虽然它可能并不理想,但一种解决方案是将 IE 的“跨域访问数据源”安全设置更改为EnablePrompt

http://technet.microsoft.com/en-us/library/dd346862.aspx

http://technet.microsoft.com/en-us/library/dd346862.aspx

In my case I was running into the same thing when trying to fetch data (via angular's $resource) from my own API that was hosted on another domain. In IE8 & IE9 these requests would fail with the error you described above. Changing the security setting in IE to allow cross-domain data requests resolved that problem.

就我而言,我在尝试从托管在另一个域上的自己的 API 中获取数据(通过 angular 的 $resource)时遇到了同样的问题。在 IE8 和 IE9 中,这些请求将因您上面描述的错误而失败。更改 IE 中的安全设置以允许跨域数据请求解决了该问题。

回答by Ant

AngularJS doesn't support IE<=9 CORS.

AngularJS 不支持 IE<=9 CORS。

As required, there is a link to github official AngualrJS website :

根据需要,有一个到 github 官方 AngualrJS 网站的链接:

AngularJS Github official discussion

AngularJS Github 官方讨论