我让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
发布评论