我在我的Nginx中有这个配置
server {
listen 8080;
add_header Access-Control-Allow-Origin *;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Content-Type';
}
现在我有我的Web应用程序为GET执行GET和POST它工作正常但是如果我执行Ajax POST我得到此错误
XMLHttpRequest cannot load ‘URL’ . Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘Origin’ is therefore not allowed access. The response had HTTP status code 404.
如果我做’GET’请求,我可以在回复中看到这一点.
Access-Control-Allow-Headers:Content-Type
Access-Control-Allow-Methods:GET, POST, OPTIONS
Access-Control-Allow-Origin:*
但如果我发帖,我看不到任何相关内容.
解决方法:
我有同样的问题,通过在我的add_header指令中添加关键字always来解决它.如documentation所述:
add_header
: Adds the specified field to a response header provided that the response code equals 200, 201, 204, 206, 301, 302, 303, 304, or 307.
[…]If the
always
parameter is specified (1.7.5), the header field will be added regardless of the response code.
发生的事情是,我总是将GET请求返回200并因此获得了预期的标头,而我的POST获得了400,因此没有标头因此触发了CORS错误.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。