简体   繁体   English

$http.post 跨域请求在 Internet Explorer 中不起作用(网络错误 0x80070005,访问被拒绝)

[英]$http.post cross domain request not working in Internet explorer (Network Error 0x80070005, Access is denied)

When I make an $http.post request and set the "withCredentials" property to true.当我发出 $http.post 请求并将“withCredentials”属性设置为 true 时。 My request works fine in Chrome and Fiefox.我的请求在 Chrome 和 Fiefox 中运行良好。 However, I'm getting the error below in IE:但是,我在 IE 中收到以下错误:

XMLHttpRequest: Network Error 0x80070005, Access is denied. XMLHttpRequest:网络错误 0x80070005,拒绝访问。

I noticed that if I enable the "Access data resources across domains" setting in IE, The error gets resolved.我注意到如果我在 IE 中启用“跨域访问数据资源”设置,错误就会得到解决。 However I need to find an alternative solution because I can't ask the users to enable that setting obviously.但是我需要找到替代解决方案,因为我不能要求用户明显启用该设置。

解决问题的 IE 设置 I noticed that a $http.get request to the same domain is working in IE with no issue, the issue is only with the $http.post request, the Options request is getting a 500 internal server and I see the request and response headers below:我注意到对同一个域的 $http.get 请求在 IE 中正常工作,问题仅在于 $http.post 请求,选项请求获得 500 内部服务器,我看到请求和响应标头以下:

IE 中的请求头 IE 中的响应头 Note: I do have the necessary custom headers, and I can see them in Chrome when the OPTIONS request succeeds.注意:我确实有必要的自定义标头,当 OPTIONS 请求成功时,我可以在 Chrome 中看到它们。 The headers that I see in Chrome are listed below:下面列出了我在 Chrome 中看到的标题:

Chrome 请求和响应标头

Could you please let me know if I'm missing something that would make the request work in IE without having to enable Access data sources across domains?如果我遗漏了一些可以使请求在 IE 中正常工作而无需跨域启用 Access 数据源的内容,请告诉我吗?

Internet Explorer 9 doesn't support cookies in CORS requests. Internet Explorer 9 不支持 CORS 请求中的 cookie。 The withCredentials property of the $http arguments attempts to send cookies. $http参数的withCredentials属性尝试发送 cookie。 I don't think there's any way to fix it with headers.我不认为有任何方法可以用标题来修复它。 IE10+ should work by default, just be sure that you are not in compatibility mode.默认情况下 IE10+应该可以工作,只需确保您未处于兼容模式。 CORS isn't fully implemented in IE10 either, but the type of request you are trying to do should work. CORS 也没有在 IE10 中完全实现,但是您尝试执行的请求类型应该可以工作。

You didn't mention what the nature of your web app is, but it impacts the type of workaround you will need for IE9.您没有提到您的 Web 应用程序的性质是什么,但它会影响 IE9 所需的解决方法类型。 If possible, see if you can refactor your code to use a GET request instead (again, I don't know what you are trying to do via AJAX so this may be impossible).如果可能,看看您是否可以重构代码以使用 GET 请求(同样,我不知道您要通过 AJAX 做什么,所以这可能是不可能的)。

You may be able to use Modernizr or something similar to detect if the browser supports CORS.您可以使用Modernizr或类似的东西来检测浏览器是否支持 CORS。 If it is not supported, send the request without AJAX and have a page refresh.如果不支持,请在不使用 AJAX 的情况下发送请求并刷新页面。

Another alternative if you really want to use AJAX is to set up a proxy on your web server, ie the server on the same domain.如果您真的想使用 AJAX,另一种选择是在您的 Web 服务器上设置代理,即同一域中的服务器。 Instead of making the cross-origin request directly, you make the AJAX request to your same-origin server, which then makes the request to the cross-origin server for you.您不是直接发出跨域请求,而是向您的同源服务器发出 AJAX 请求,然后该服务器会为您向跨域服务器发出请求。 The server won't have CORS issues.服务器不会有 CORS 问题。 This solution assumes, of course, that you have some server-side scripting going on such as PHP, Node or Java.当然,此解决方案假设您有一些服务器端脚本,例如 PHP、Node 或 Java。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM