CORS 和非 200 statusCode

编程入门 行业动态 更新时间:2024-10-25 19:29:36
本文介绍了CORS 和非 200 statusCode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的 CORS 与 Nginx 配合良好.API 旨在发送非 HTTP 200 状态代码,例如,401、404 等,以防输入错误.问题是 Chrome 会在收到非 HTTP 200 状态代码时取消/中止请求.由于这个原因,我无法在 Web 客户端上显示确切的错误.

I have CORS working well with Nginx. The APIs are designed to send non HTTP 200 status code, for example, 401, 404, etc., in case of bad input. The problem is that Chrome cancels/abort the request if it receives a non HTTP 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:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634266.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:CORS   statusCode

发布评论

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

>www.elefans.com

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