Access to XMLHttpRequest at 'xxx' from origin 'xxx' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed.
背景: 我的后端使用了 Django
+ corsheaders
允许跨域, 而昨天同事在配置 Nginx
时, 将整个 server
块都添加了跨域支持, 也就是在同一个响应报文里, Access-Control-Allow-Origin
头被设置了两次(corsheaders
设置一次, 在nginx
中又被设置了一次), 也就成为了多值(本应该是'*'
, 实际返回的是'*, *'
), 导致浏览器禁止跨域.
解决方案就是把 Nginx
中的 add_header Access-Control-Allow-Origin *;
指令, 应用在同事的 location
块而非整个 server
块, 恢复正常.