nginx + django 添加了django-cors-headers后, 本地请求服务器上还是跨域,这里只需要在nginx的配置文件中添加下面几行即可
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
这里指的是通用方法,如果需要对特定的请求方式添加头,可以参考这篇文章,以上方法博主亲测有效!
https://www.fujieace.com/nginx/cors-2.html