为什么 CORS 请求在 Microsoft Edge 中失败但在其他浏览器中有效?

Why are CORS requests failing in Microsoft Edge but working in other browsers?

我正在使用 jQuery 发送跨源 ajax 请求,它们在 IE11、Chrome 和 Firefox 中工作正常,但它们在 Edge 中失败并出现以下错误:

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

有趣的是,我使用 Fiddler 试图弄清楚发生了什么以及 Fiddler 何时 运行 并且捕获请求一切正常。一旦我关闭 Fiddler 或暂停捕获,它就会再次失败。

该站点 运行 在我的本地计算机 (webpack-dev-server) 上通过本地网络向 WebAPI 服务发出请求。

我的主机文件是这样设置的:

127.0.0.1   local.myapp.test
192.168.0.111   api.myapp.test

在生产环境中这应该不是问题,因为站点和 API 将托管在同一个地方,但它对于开发和测试来说非常宝贵。


更新:

感谢 Eric Law on the Telerik forums 我现在知道为什么它在启用 Fiddler 时表现不同 - Edge 正在切换到本地 Intranet 区域,因为 Fiddler 更改了代理设置并且 Intranet 区域具有较低的安全级别。

There are a few possibilities; without exact details of your configuration, we're just shooting in the dark.

One possibility is that your computer is configured with an Intranet zone and that Intranet zone is dependent on a proxy configuration script: http://blogs.msdn.com/b/ieinternals/archive/2012/06/05/the-local-intranet-security-zone.aspx. When Fiddler is running, the proxy settings are pointed at Fiddler itself.

我打算将本地 Intranet 区域安全级别提高到中高以匹配 Internet 区域,然后使用 Fiddler 尝试找出 Edge 对 CORS 请求感到不安的原因。

在 Edge 的 about:flags 中,确保选中 "Allow localhost loopback (this might put your device at risk) "。

我将在下面逐字逐句地包括 Eric Lawrence(Fiddler 的创建者)在 Fiddler 论坛上提供的答案:

One possibility is that your computer is configured with an Intranet zone and that Intranet zone is dependent on a proxy configuration script: http://blogs.msdn.com/b/ieinternals/archive/2012/06/05/the-local-intranet-security-zone.aspx. When Fiddler is running, the proxy settings are pointed at Fiddler itself.

... there's another factor at work here if you're using an Intranet site as the target of an XHR from a site in the Internet zone.

Edge runs in Enhanced Protected Mode (AppContainer). That has a feature which blocks access to Private Network Resources from Internet-Zone processes. See the "Private Network resources" section of http://blogs.msdn.com/b/ieinternals/archive/2012/03/23/understanding-ie10-enhanced-protected-mode-network-security-addons-cookies-metro-desktop.aspx for more details.

我将 local.myapp.test(URL 我是 运行 我的 SPA 来自)添加到 Internet 选项中的本地 Intranet 区域,现在 Edge 不需要 Fiddler 了。

我遇到了这个问题,在尝试了几个选项之后,对我有用的方法是从所有区域站点条目中删除我正在使用的域。以 local.myapp.test 为例,我检查了任何 "anysubdomain".myapp.test 条目,并将它们从所有区域中删除,包括所有子域或通配符条目。

在 Internet 选项 (IE 11) 中,select 安全 选项卡,然后在“本地 Intranet”中单击“sites”然后是“A​​dvanced”并删除了其中的相关域引用。

在“受信任的站点”中单击“站点”并从其中的列表中删除相关条目

我最近遇到了这个 'issue',解决方法是转到 edge://flags/(只需在搜索栏中输入)。之后,搜索 CORS,很快就会看到 Block insecure private network requests 标志。您所要做的就是 select disable 就是这样。