每当CORS $ http请求失败时,返回的响应始终为0

时间:2022-08-23 11:58:44

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.

这是SO中这个帖子的类似问题。但是这里提供的答案不能应用于我的情况,因为我无法更改服务器的响应头。

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.

假设http POST到不同的来源。这意味着CORS,包括CORS预检交换。现在假设OPTIONS请求由于服务器问题而返回503服务不可用错误。在这种情况下,错误处理程序为数据提供“”,为状态提供0而不是给我状态代码503和文本:服务不可用。下图显示了此场景的一个示例。

每当CORS $ http请求失败时,返回的响应始终为0

每当CORS $ http请求失败时,返回的响应始终为0

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.

我正在使用angularjs $ http,我知道在响应中没有CORS头,如果发生这样的错误。我无法改变它。

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

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

1 个解决方案

#1


10  

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 / $ http的问题,但它是浏览器及其XMLHttpRequest对象的行为:如果CORS请求失败,浏览器不会将任何信息提供给调用者。

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.

在我理解之前,我也认为它是一个AngularJS问题,我在Angular的github上提出了一个未解决的问题 - >以及相应的评论。

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

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.

所以我认为似乎没有其他方法可以解决这个问题,因为在503的情况下也在代理/负载均衡器上添加了Access-Control-Allow-Origin响应头。

Edit: 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.

编辑:如果您的负载均衡器是HAproxy,以下内容也可能对您有所帮助:HAproxy:OPTIONS和POST方法的不同503错误文件它显示了如何让HAproxy使CORS请求自动化。

#1


10  

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 / $ http的问题,但它是浏览器及其XMLHttpRequest对象的行为:如果CORS请求失败,浏览器不会将任何信息提供给调用者。

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.

在我理解之前,我也认为它是一个AngularJS问题,我在Angular的github上提出了一个未解决的问题 - >以及相应的评论。

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

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.

所以我认为似乎没有其他方法可以解决这个问题,因为在503的情况下也在代理/负载均衡器上添加了Access-Control-Allow-Origin响应头。

Edit: 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.

编辑:如果您的负载均衡器是HAproxy,以下内容也可能对您有所帮助:HAproxy:OPTIONS和POST方法的不同503错误文件它显示了如何让HAproxy使CORS请求自动化。