【问题标题】:Django-cors-headers not workingDjango-cors-headers 不工作
【发布时间】:2016-06-08 00:29:07
【问题描述】:

我的 django 版本是 1.8.6。我已将 corsheaders 文件夹复制到项目文件夹中。我已经 pip install django-cors-headers(ver 1.1.0)。这是我的设置.py:

    INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'MyWebsite_app',
    'storages',
    'rest_framework',
    'corsheaders',
)

MIDDLEWARE_CLASSES = (
    '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',
    'django.middleware.security.SecurityMiddleware',
)

CORS_ORIGIN_ALLOW_ALL = True

这是我的 jquery:

function getLeague() {
$.ajax({
    url: 'http://otherdomain.ashx?username=xxx&password=xxx&sportsBook=xxx&sportsType=xxx&gameType=xxx',
    type: 'GET',
    dataType: 'json',
    success: function(data) {
        alert('Success');
    },
    error: function(data) {
        alert('Fail');
    }
    });
}

在执行 getLeague() 时,它会一直提示“失败”。当我看到控制台时,它显示“XMLHttpRequest 无法加载http://otherdomain.ashx?username=xxx&password=xxx&sportsBook=xxx&sportsType=xxx&gameType=xxx。请求的源上不存在 Access-Control-Allow-Origin 标头”。我应该在 urls.py 还是 view.py 中添加一些代码?谢谢。

【问题讨论】:

  • dataType: 'jsonp', 或者您可以发布网址的输出吗?
  • @Jai 我也试过 jsonp。但它也不起作用。这是返回示例: {"Msg":"","t":1456383442,"IsFullRefresh":true,"Refresh":[{"UID":"3e9b72f916de06f619ebeb8493f34817","CID":"1dcdbcab"}]}我已经检查了 jsonlin.com,它是一个有效的 json。
  • 我不在 django 应用程序上工作,但你这里有一个尾随逗号 `'corsheaders',`。
  • @Jai 谢谢你的回复。但在 django 中,尾随逗号不会有问题。我认为那不是问题
  • 尝试从MIDDLEWARE_CLASSES注释掉'django.middleware.clickjacking.XFrameOptionsMiddleware',

标签: jquery python ajax django django-cors-headers


【解决方案1】:

最好在您的应用程序中创建一个代理,该代理将调用另一个域并返回数据:

function getLeague() {
  $.ajax({
    url: '/crossdomainData',
    type: 'GET',
    dataType: 'json',
    success: function(data) {
        alert('Success');
    },
    error: function(data) {
        alert('Fail');
    }
    });
}

当你使用 django 时,你可以导入这个Django HTTP Proxy

简介

Django HTTP 代理为 Django Web 开发框架提供了简单的 HTTP 代理功能。 它允许您通过从运行 Django 应用程序的主服务器请求外部服务器来向外部服务器发出请求。此外,它允许您记录对这些请求的响应并播放他们随时回来。


另一个选项在这里taken from this post@dvcrn 回答。

import urllib2
    def crossdomainData(request):
        url = "http://otherdomain.ashx?username=xxx&password=xxx&sportsBook=xxx&sportsType=xxx&gameType=xxx"
        req = urllib2.Request(url)
        response = urllib2.urlopen(req)
        return HttpResponse(response.read(), content_type="application/json")

【讨论】:

  • 不错的一个。这不会增加主服务器的开销吗?
  • @Wtower 对不起,我不太了解服务器的工作原理。但是,实际上我没有安装 django-http-proxy。我只是按照 Jai 给出的功能。即使没有安装 django-http-proxy 我也可以工作。
【解决方案2】:

大约 500 个错误发生在 CORS 中间件之前,因此它没有机会添加 CORS 标头。如果响应状态码是 500,这可能是问题所在,CORS 可能工作正常。

【讨论】:

    猜你喜欢
    • 2016-11-16
    • 2018-06-23
    • 2018-01-16
    • 2015-03-18
    • 2018-04-24
    • 2018-08-21
    • 2020-06-11
    • 2017-08-11
    • 2020-01-08
    相关资源
    最近更新 更多