当使用Phonegap访问时,在Django应用程序中的Access-Control-Allow-Origin [英] Access-Control-Allow-Origin in Django app when accesed with Phonegap
问题描述
我正在为基于Django的应用程序开发一个Phonegap应用程序,但是当尝试进行Ajax调用时,我收到此错误:
I'm developing a Phonegap app for my Django based app, but when trying to make Ajax calls I get this error:
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应用允许某些网址的跨源?
How can I make it so my Django app allows cross origin for some urls?
这是我的Ajax代码:
Here's my Ajax code:
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/ottoyiu/django -cors-headers
Django by default does not provide the headers necessary to provide cross origin. The easiest way would be to just use this Django app that handles it for you: https://github.com/ottoyiu/django-cors-headers
然后,您可以使用设置设置要列入白名单的域。
You can then set whichever domains you want white listed using the settings
CORS_ORIGIN_WHITELIST = (
'google.com',
'hostname.example.com'
)
支持允许所有,只需使用设置...
CORS_ORIGIN_ALLOW_ALL = True
,然后在中间件或视图中对请求进行任何过滤。
to support allowing all, just use the setting...
CORS_ORIGIN_ALLOW_ALL = True
and then do any filtering of the request in middleware or in the view.
这篇关于当使用Phonegap访问时,在Django应用程序中的Access-Control-Allow-Origin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!