加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax – Nginx CORS不适用于POST

发布时间:2020-12-16 02:48:54 所属栏目:百科 来源:网络整理
导读:我在我的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执
我在我的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,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错误.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读