PUT请求 出现401,且Request Method变成了OPTIONS。No 'Access-Control-Allow-Origin' header

心已入冬 提交于 2019-12-19 01:38:57

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

 

发送PUT的请求,出现了OPTIONS请求的跨域错误,这是为什么呢?

原来是跨域的时候,对于PUT、POST、DELETE请求,浏览器会先发起一个options请求进行测试连接。当options请求返回200之后才会正式发起PUT、POST、DELETE请求。

所以解决这个问题就是保证options请求测试连接是正常的。

解决方法:

1.如果使用nginx,则配置对options请求测试连接的处理


        location ^~ /manage-api/ {

            if ($request_method = 'OPTIONS') {

                add_header Access-Control-Allow-Origin $http_origin always;
                add_header Access-Control-Allow-Credentials true always;
                add_header Access-Control-Allow-Methods 'GET,POST,PUT,DELETE,OPTIONS' always;
                add_header Access-Control-Allow-Headers 'Authorization,X-Requested-With,Content-Type,Origin,Accept' always;
                add_header Access-Control-Max-Age 3600;
                add_header Content-Length 0;
                return 200;
            }

           proxy_pass   http://localhost:8068;

    }

2.也可以在代码中对OPTIONS请求进行过滤,直接返回200

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!