CORS和非200 statusCode [英] CORS and non 200 statusCode

查看:163
本文介绍了CORS和非200 statusCode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我让CORS与Nginx运作良好.这些API设计为针对错误的输入发送非200状态代码(例如401、404等).问题在于,如果Chrome收到非200状态代码,则会取消/中止该请求.由于这个原因,我无法在Web客户端上显示确切的错误.

I have CORS working well with Nginx. The APIs are design to send non 200 status code - example 401, 404 etc - for bad input. The problem is that Chrome cancels / abort the request if it receives a non 200 status code. Due to this reason, I am not able to show the exact error on the Web client.

如何解决CORS非200状态代码错误?

What is the way around for CORS non 200 status code errors?

推荐答案

默认情况下,Nginx仅为认为成功的请求添加标头.您可以通过将always参数添加到您的add_header指令(例如

By default Nginx only adds headers for requests it considers successful. You can make it add the header without regard for the response code, by adding the always parameter to your add_header directive, e.g.

add_header 'Access-Control-Allow-Origin' '*' always;

仅将'always'参数添加到'Access-Control-Allow-Origin'标头中是不够的.需要将"always"参数始终添加到需要始终添加的标头中.在某些情况下,您需要将'always'参数添加到'Access-Control-Allow-Credentials'标头中.

Adding the 'always' parameter to the 'Access-Control-Allow-Origin' header is not enough. The 'always' parameter needs to be added to the headers that need to be always added. In some cases you will need to add the 'always' parameter to the 'Access-Control-Allow-Credentials' header.

add_header 'Access-Control-Allow-Credentials' 'true' always;

这篇关于CORS和非200 statusCode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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