每当CORS $ HTTP请求失败,返回的响应始终为0 [英] Whenever a CORS $http request fails, the response returned is always 0

查看:539
本文介绍了每当CORS $ HTTP请求失败,返回的响应始终为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个类似的问题本<一个href=\"http://stackoverflow.com/questions/25506165/angularjs-http-returns-status-$c$c-0-from-failed-cors-request\">post在这里SO.But提供的答案不能在我的情况下被应用,因为我无法从服务器更改响应头。

this is a similar question to this post in SO.But the answer provided here cannot be applied in my case as i cannot change the response headers from server.

假设一个HTTP POST到不同的起源。这意味着CORS,包括CORS preflight交流。现在假设OPTIONS请求,由于服务器问题返回 503服务不可用错误。在这种情况下,错误处理程序提供了数据和 0 的状态,而不是给我的状态$ C $ ç503和文本:服务不可用。此方案的一个例子在下面的图像中所示。

Suppose an http POST to a different origin. This implies CORS, including a CORS preflight exchange. Now suppose the OPTIONS request returns a 503 service unavailable error due to a server problem. In this case the error handler gives ""for data and 0 for status instead of giving me the status code 503 and the text:service not available. An example of this scenario is illustrated in the below image.

我使用angularjs $ HTTP,我知道在响应没有CORS头,如果这样的错误happen.and我不能改变它。

I am using angularjs $http and i know in the response there are no CORS header if such errors happen.and i cannot change it.

有什么办法,我可以收到正确的错误code,在我拒绝对象中的文本。

Is there any way i can receive the proper error code and the text in my rejection object.

推荐答案

这是不是AngularJS / $ HTTP的一个问题,但它是浏览器和其XMLHtt prequest对象的行为:如果CORS请求失败,浏览器没有任何信息回馈给调用者。

This is not an issue of AngularJS / $http but it is the behavior of the browsers and their XMLHttpRequest object: If the CORS request fails, the browser does not give any information back to the caller.

在我得到了这样的认识,我也尽管它是一个AngularJS的问题,我对角github上提出的一个悬而未决的问题 - >用相应的注释

Before I got this understanding, I also though it to be an AngularJS issue and I raised an open issue on github of Angular -> with the corresponding comment.

https://github.com/angular/angular.js/问题/ 13085#issuecomment-148047721

所以我觉得似乎没有其他的方式来解决这个问题,以添加访问控制允许来源响应头还上的情况下,代理/负载均衡503。

So I think there seems no other way to solve this as to add the Access-Control-Allow-Origin response header also on the proxy / load balancer in case of 503.

编辑:
如果你的负载平衡器是一个HAProxy的,下面可以帮助你太:结果
<一href=\"http://stackoverflow.com/questions/34022378/haproxy-different-503-errorfile-for-options-and-post-methods/34071533#34071533\">HAproxy:不同的503错误文件选项和POST方法结果
它展示了如何让HAProxy的前面回答的CORS请求自治区。

If your load balancer is a HAproxy, the following may help you too:
HAproxy: different 503 errorfile for OPTIONS and POST methods
It shows how to let HAproxy anwer the CORS requests autonomous.

这篇关于每当CORS $ HTTP请求失败,返回的响应始终为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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