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

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

问题描述

这是一个与此

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

如果您的负载均衡器是 HAproxy,以下内容也可能对您有所帮助:
HAproxy:OPTIONS 和 POST 方法的不同 503 错误文件
它展示了如何让 HAproxy 自主回答 CORS 请求.

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.

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.

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.

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

解决方案

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.

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/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.

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.

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

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