Django-cors-headers不起作用 [英] Django-cors-headers not working
问题描述
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');
},
错误:function(data){
alert('Fail');
}
});
}
执行getLeague()时,它会保持警报Fail。当我看到控制台时,显示XMLHttpRequest无法加载 http://otherdomain.ashx?username = xxx& password = xxx& sportsBook = xxx& sportsType = xxx& gameType = xxx 。请求源上没有Access-Control-Allow-Origin标头。我应该在urls.py或view.py中添加一些代码吗?谢谢。
更好地在您的应用程序中创建一个代理,这又会调用另一个域,并将返回您的数据:
function getLeague(){
$ .ajax({
url:'/ crossdomainData'
类型:'GET',
dataType:'json',
success:function(data){
alert('Success');
},
error:function(data){
alert('Fail');
}
});
}
正如使用django一样,您可以导入这个 Django HTTP代理 。
简介
Django HTTP Proxy为Django网站提供简单的HTTP代理功能开发框架。 它允许您通过从运行Django应用程序的主服务器请求来向外部服务器发出请求。 此外,它允许您记录对这些请求的响应并播放他们随时回来
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)
My django version is 1.8.6. I've copy the corsheaders folder into the project folder. i've pip install django-cors-headers(ver 1.1.0). This is my 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
This is my 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');
}
});
}
It keeps alerting "Fail" when executing the getLeague(). And when i see the console it shows "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". Should i add some code in the urls.py or in the view.py? Thank you.
Better to create a proxy at your application which in turn will call the other domain and will return you the data:
function getLeague() {
$.ajax({
url: '/crossdomainData',
type: 'GET',
dataType: 'json',
success: function(data) {
alert('Success');
},
error: function(data) {
alert('Fail');
}
});
}
As you are using django, you can import this 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.
Another option is here 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")
这篇关于Django-cors-headers不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!