jQuery CORS Django '访问控制-允许-来源'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35521935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CORS django 'Access-Control-Allow-Origin'
提问by Nathan
I was trying to get a CORS request working. With the following JS code I get this error: XMLHttpRequest cannot load http://localhost:65491/?token=u80h9kil9kjuu02539buak4r6n&user=~me. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:50303' is therefore not allowed access.
我试图让 CORS 请求工作。使用以下 JS 代码,我收到此错误:XMLHttpRequest cannot load http://localhost:65491/?token=u80h9kil9kjuu02539buak4r6n&user=~me. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:50303' is therefore not allowed access.
this is the JS code:
这是JS代码:
$.ajax({
url: "http://localhost:60906/",
data: {token : 'u80h9kil9kjuu02539buak4r6n', user : '~me'},
type: "GET",
crossDomain: true,
success: function( response ) {
alert('Success!' + response);
var context = response;
}
});
When I look at the network using chrome's devtools I see that there is no 'Access-Control-Allow-Origin'
header indeed. But when I load the site manually it is present!
当我使用 chrome 的 devtools 查看网络时,我发现'Access-Control-Allow-Origin'
确实没有标题。但是当我手动加载网站时,它就出现了!
I used the following code to set the headers:
我使用以下代码来设置标题:
response = JsonResponse(simpleWeek)
response['Access-Control-Allow-Origin'] = '*'
return response
hoping for some help!
希望得到一些帮助!
回答by T. Opletal
It says No 'Access-Control-Allow-Origin' header is present on the requested resource.
which means your server application needs tunning to accept cross origin requests.
Cross origin requests are by default not working due to security reasons. You need to enable them.
它说No 'Access-Control-Allow-Origin' header is present on the requested resource.
这意味着您的服务器应用程序需要调整以接受跨源请求。由于安全原因,默认情况下跨源请求不起作用。您需要启用它们。
For django there is a maintained package with good amount of settings just for this: https://github.com/ottoyiu/django-cors-headers/
对于 django,有一个维护包,其中包含大量设置:https: //github.com/ottoyiu/django-cors-headers/
回答by Seyhak Ly
After 2 hours of troubleshooting I found solution: TYPOin url. Check twice, maybe it will fix your issue too.
经过 2 小时的故障排除后,我找到了解决方案:网址中的TYPO。检查两次,也许它也会解决您的问题。
回答by Pronay Guha
For this thing to work you need to do two thing:
为了让这件事起作用,你需要做两件事:
instead of
https://
just givehttp://
in your CORS_ORIGIN_WHITELISTin settings.pyadd
CORS_ORIGIN_ALLOW = True
in the same file
而不是
https://
只给http://
你CORS_ORIGIN_WHITELIST在settings.pyCORS_ORIGIN_ALLOW = True
在同一个文件中添加