简体   繁体   English

在Heroku上的Django应用中为CORS指定URLS

[英]Specify URLS for CORS in Django app on Heroku

I have an app in Heroku running with Django. 我在Heroku中有一个与Django一起运行的应用程序。

Now I'm starting to develop a Phonegap app that I want to make work with my Heroku app. 现在,我开始开发一个我想与Heroku应用程序一起使用的Phonegap应用程序。

I can't make it work because of CORS (Cross-origin resource sharing) protection. 由于CORS(跨域资源共享)保护,我无法使其工作。 So I need to exclude some urls (not all app) to make my Phonegap app work. 因此,我需要排除一些网址(并非所有应用程序)以使我的Phonegap应用程序正常工作。

I've tried installing django-cors-headers , but it doesn't seem to work. 我试过安装django-cors-headers ,但是似乎没有用。

To test it, I'm making a call to obtain a csrf_token. 为了测试它,我正在打电话获取csrf_token。

I added this to my setting.py (and of course followed the guide, setting all to default): 我将其添加到我的setting.py(当然也遵循指南,将所有设置都设置为默认值):

CORS_URLS_REGEX = r'^register/.*$'
CORS_URLS_REGEX = r'^login/.*$'
CORS_URLS_REGEX = r'^getcsrf/.*$'

And this is the Ajax call I try to make: 这是我尝试进行的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);
            });
    });
}

But I get this marked in red as an error and an empty response field: 但是我将其标记为红色,作为错误和一个空的响应字段:

GET http://domain.herokuapp.com/getcsrf/?tags=jquery%2Cjavascript&tagmode=any&format=json   200 OK    206ms

There's no cross origin restrictions with phonegap, it's using a webview, not a web browser. phonegap没有跨源限制,它使用的是Webview,而不是Web浏览器。

The only thing you have to do with phonegap/cordova is to be sure your server is whitelisted in config.xml. 您与phonegap / cordova唯一要做的就是确保您的服务器已在config.xml中列入白名单。

In cordova 3.3/3.4, the default setting is <access origin="*" /> which should allow access to any url. 在cordova 3.3 / 3.4中,默认设置为<access origin="*" /> ,它应允许访问任何URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM