CORS和非200 statusCode

编程入门 行业动态 更新时间:2024-10-25 15:35:56
本文介绍了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

本文发布于:2023-11-26 15:25:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634264.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:CORS   statusCode

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!