我的django版本是1.8.6。我将corsheaders文件夹复制到项目文件夹中。我已经安装了django-cors-headers(版本1.1.0)。这是我的setting.py:
= [NSMutableArray new];
_createdLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 100, 20)];
_createdLabel.backgroundColor = [UIColor clearColor];
_createdLabel.textColor = [UIColor whiteColor];
UIFont *myFont = [ UIFont fontWithName:@"GothamPro-Light" size:15.0f ];
_createdLabel.font = myFont;
_createdLabel.text = @"";
UIBarButtonItem *createdTitle = [[UIBarButtonItem alloc] initWithCustomView:_createdLabel];
[_items addObject:createdTitle];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[_items addObject:space];
UILabel *shareLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, -1000, 85, 20)];
shareLabel.backgroundColor = [UIColor clearColor];
shareLabel.textColor = [UIColor whiteColor];
UIFont *shareFont = [ UIFont fontWithName:@"GothamPro-Light" size:15.0f ];
shareLabel.font = shareFont;
shareLabel.text = @"Поделиться";
[shareLabel sizeToFit];
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(socialSharing:)];
[shareLabel setUserInteractionEnabled:YES];
[shareLabel addGestureRecognizer:tap];
UIBarButtonItem *shareTitle = [[UIBarButtonItem alloc] initWithCustomView:shareLabel] ;
[_items addObject:shareTitle];
UIBarButtonItem *shareBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(socialSharing:)];
shareBtn.tintColor = [UIColor whiteColor];
[_items addObject:shareBtn];
[self.toolBar setItems:_items animated:YES];
这是我的jquery:
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
它一直警告"失败"执行getLeague()时。当我看到它显示的控制台时," XMLHttpRequest无法加载http://otherdomain.ashx?username=xxx&password=xxx&sportsBook=xxx&sportsType=xxx&gameType=xxx。请求的源"上没有Access-Control-Allow-Origin标头。我应该在urls.py或view.py中添加一些代码吗?谢谢。
答案 0 :(得分:2)
最好在您的应用程序中创建代理,然后再调用其他域并返回数据:
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 Proxy为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")
答案 1 :(得分:0)
早于CORS中间件发生了大约500个错误,因此没有机会添加CORS标头。如果响应状态代码为500,则可能是问题,CORS可能正常工作。