Django 1.9 + django-cors-headers + AJAX不工作 [英] Django 1.9 + django-cors-headers + AJAX not working

查看:267
本文介绍了Django 1.9 + django-cors-headers + AJAX不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的Django应用程序之间进行跨站点通信,该应用程序返回json和运行AJAX的客户端。

I'm trying to do a Cross-Site communication between my Django app that returns json and a client running AJAX.

我正在使用 django-cors-headers ,具有以下配置:

I'm using django-cors-headers with the following configuration:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp.apps.MyAppConfig',
    'corsheaders',
]

MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',

]


CORS_ORIGIN_ALLOW_ALL = True

现在我试图让所有的连接只是进行测试。
这是我的观点:

For now I'm trying to allow all connections just to make tests. This is my view:

def index(request):
    if request.is_ajax:     
        results={}
        results=["The password is valid"]
        data_json=json.dumps(results)

    mimetype="application/json"
    return HttpResponse(data_json,mimetype)

我也尝试过装饰器@csrf_exempt。
最后,这是我的AJAX:

I tried with the decorator @csrf_exempt too. And finally, this is my AJAX:

$.ajax({
    url: "http://otherdomain.com/myapp/",
    type: "POST",
    crossDomain: true,
    data :{ 
            'username': $("#myName").val(),
        },
    dataType: "json",
    success:function(result){
        alert(JSON.stringify(result));
    },
    error:function(xhr,status,error){
        alert(status);
    }
});

控制台正在警告:


XMLHttpRequest无法加载 http://otherdomain.com/myapp/ 。请求资源上不存在Access-Control-Allow-Origin头。原来'thisdomain.com'因此不被允许访问。

XMLHttpRequest cannot load http://otherdomain.com/myapp/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'thisdomain.com' is therefore not allowed access.

任何帮助将是有用的。非常感谢你。

Any help will be useful. Thank you very much.

推荐答案

从你的意见我可以认为你忘了在你的网页服务器/主机上启用cors支持。当您使用Dreamhost时,您可以参考其文档 here

From your comments I can think that you forgot to enable cors support on your web server/hosting. As you use Dreamhost, you can refer to it's documentation here.

另外,作为@YPCrumble在他的答案中所说的当您有工作请求时,使用最新版本的 Django-cors-middleware

Also as @YPCrumble said in his answer you might want to use up to date fork Django-cors-middleware when you will have working requests.

这篇关于Django 1.9 + django-cors-headers + AJAX不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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