Nginx XMLHttpRequest CORS请求失败,即使使用访问控制允许源标头也是如此
我在进行跨站点ajax调用时遇到问题。使用nginx,我相信我已经在服务器配置中添加了正确的头,但它在我的JS代码或控制台中仍然不起作用。我不知道我做错了什么 以下是我在控制台中键入的内容。响应是常见的“No‘Access Control Allow Origin’header is present”错误: 当我查看初始网页加载的响应标题时,我确实看到了以下标题:Nginx XMLHttpRequest CORS请求失败,即使使用访问控制允许源标头也是如此,nginx,xmlhttprequest,cors,Nginx,Xmlhttprequest,Cors,我在进行跨站点ajax调用时遇到问题。使用nginx,我相信我已经在服务器配置中添加了正确的头,但它在我的JS代码或控制台中仍然不起作用。我不知道我做错了什么 以下是我在控制台中键入的内容。响应是常见的“No‘Access Control Allow Origin’header is present”错误: 当我查看初始网页加载的响应标题时,我确实看到了以下标题: Access-Control-Allow-Credentials:true Access-Control-Allow-Headers
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:X-Requested-With,Accept,Content-Type, Origin
Access-Control-Allow-Methods:GET, POST, OPTIONS, PUT, DELETE
Access-Control-Allow-Origin:*
以下是该站点的nginx配置文件:
server {
listen 80;
root /home/aherriot/sites/dictionary;
index index.html index.htm;
server_name dictionary.aherriot.com;
location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
add_header 'Access-Control-Allow-Headers' 'X-Requested-With,Accept,Content-Type, Origin';
}
}
我被难住了,不知道我错过了什么。我还需要做什么才能允许CORS?谢谢。您可以使用
*
接受所有来源,但这将阻止使用访问控制允许凭证:true'
请看这里的更多示例-问题在于对CORS的定义存在根本性的误解。不是由源服务器指定页面可以通过XMLHttpRequest请求访问哪些第三方域,而是由外部域上的服务器指定哪些域可以连接到它
server {
listen 80;
root /home/aherriot/sites/dictionary;
index index.html index.htm;
server_name dictionary.aherriot.com;
location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
add_header 'Access-Control-Allow-Headers' 'X-Requested-With,Accept,Content-Type, Origin';
}
}