我正在为基于 Django 的应用程序开发 Phonegap 应用程序,但是当尝试进行 Ajax 调用时,我收到此错误:
XMLHttpRequest cannot load http://domain.herokuapp.com/getcsrf/?tags=jquery%2Cjavascript&tagmode=any&format=json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
我怎样才能让我的 Django 应用程序允许某些 url 跨源?
这是我的 Ajax 代码:
get: function() {
$.getJSON("http://domain.herokuapp.com/getcsrf/",
{
tags: "jquery,javascript",
tagmode: "any",
format: "json"
},
function(data) {
$.each(data.items, function(item){
console.log(item);
});
});
}
默认情况下,Django 不提供跨源所需的标头。最简单的方法是使用这个 Django 应用程序来为您处理它:https://github.com/adamchainz/django-cors-headers https://github.com/adamchainz/django-cors-headers
- 添加到已安装的应用程序
- 添加到中间件
- 然后诸如此类的事情...
CORS_ALLOWED_ORIGINS = [
"http://read.only.com",
"http://change.allowed.com",
]
要支持允许所有,只需使用设置...CORS_ALLOW_ALL_ORIGINS = True
然后在中间件或视图中对请求进行任何过滤。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)