访问控制 - 允许 - 原产地在Django应用程序时的PhoneGap accesed [英] Access-Control-Allow-Origin in Django app when accesed with Phonegap

查看:162
本文介绍了访问控制 - 允许 - 原产地在Django应用程序时的PhoneGap accesed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序的PhoneGap为我的Django的基于应用程序,但试图让当Ajax调用我得到这个错误:

  XMLHtt prequest无法加载http://domain.herokuapp.com/getcsrf/?tags=jquery%2Cjavascript&tagmode=any&format=json。没有访问控制 - 允许 - 原产地标头的请求的资源present。原产地'空',因此没有允许访问。
 

我怎样才能使它所以我的Django应用程序允许跨产地为一些网址?

下面是我的Ajax code:

 得到:函数(){
    $ .getJSON(http://domain.herokuapp.com/getcsrf/
    {
        标签:jQuery的,JavaScript的,
        tagmode:任何,
        格式为:JSON
    },
    功能(数据){
        $每个(data.items,函数(项目){
            执行console.log(项目);
            });
    });
}
 

解决方案

Django的默认情况下不提供必要的头文件,以提供跨原点。最简单的方法是只使用Django应用程序来处理它为您:的https:// github上。 COM / ottoyiu / Django的-CORS报头

您可以再设置要使用的设置列入白名单为准域

  CORS_ORIGIN_WHITELIST =(
    google.com,
    hostname.example.com
)
 

支持允许所有的,只是使用的设置... CORS_ORIGIN_ALLOW_ALL = TRUE 然后执行在中间件或视图中的请求的任何滤波

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. 

How can I make it so my Django app allows cross origin for some urls?

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 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'
)

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.

这篇关于访问控制 - 允许 - 原产地在Django应用程序时的PhoneGap accesed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆