Django-cors-headers 不工作
Django-cors-headers not working
我的django版本是1.8.6。我已将 corsheaders 文件夹复制到项目文件夹中。我已经 pip 安装了 django-cors-headers(版本 1.1.0)。这是我的 setting.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() 时一直提示 "Fail"。当我看到控制台时,它显示 "XMLHttpRequest cannot load http://otherdomain.ashx?username=xxx&password=xxx&sportsBook=xxx&sportsType=xxx&gameType=xxx. No Access-Control-Allow-Origin header is present on the requested source"。我应该在 urls.py 还是 view.py 中添加一些代码?谢谢。
最好在您的应用程序中创建一个代理,该代理将调用另一个域并 return 您将获得数据:
function getLeague() {
$.ajax({
url: '/crossdomainData',
type: 'GET',
dataType: 'json',
success: function(data) {
alert('Success');
},
error: function(data) {
alert('Fail');
}
});
}
由于您使用的是 django,因此可以导入此 Django HTTP Proxy。
Introduction
Django HTTP Proxy provides simple HTTP proxy functionality for the Django web development framework. It allows you make requests to an external server by requesting them from the main server running your Django application. In addition, it allows you to record the responses to those requests and play them back at any time.
这里有另一个选项taken from this post answered by @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")
比CORS中间件更早发生500个错误,所以它没有机会添加CORS头。如果响应状态代码是 500,这可能是问题所在,CORS 可能工作正常。
我的django版本是1.8.6。我已将 corsheaders 文件夹复制到项目文件夹中。我已经 pip 安装了 django-cors-headers(版本 1.1.0)。这是我的 setting.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() 时一直提示 "Fail"。当我看到控制台时,它显示 "XMLHttpRequest cannot load http://otherdomain.ashx?username=xxx&password=xxx&sportsBook=xxx&sportsType=xxx&gameType=xxx. No Access-Control-Allow-Origin header is present on the requested source"。我应该在 urls.py 还是 view.py 中添加一些代码?谢谢。
最好在您的应用程序中创建一个代理,该代理将调用另一个域并 return 您将获得数据:
function getLeague() {
$.ajax({
url: '/crossdomainData',
type: 'GET',
dataType: 'json',
success: function(data) {
alert('Success');
},
error: function(data) {
alert('Fail');
}
});
}
由于您使用的是 django,因此可以导入此 Django HTTP Proxy。
Introduction
Django HTTP Proxy provides simple HTTP proxy functionality for the Django web development framework. It allows you make requests to an external server by requesting them from the main server running your Django application. In addition, it allows you to record the responses to those requests and play them back at any time.
这里有另一个选项taken from this post answered by @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")
比CORS中间件更早发生500个错误,所以它没有机会添加CORS头。如果响应状态代码是 500,这可能是问题所在,CORS 可能工作正常。