【发布时间】:2014-03-10 22:19:03
【问题描述】:
我正在使用 python2.7、django1.6 和 apache2。我启用了跨域访问。我尝试过使用和不使用 crsf 令牌。我不知道我做错了什么。
访问: url/to/site/contacts/api/v1/adresponses/1 正常工作,因此设置了美味的派。我可以看到广告响应。
请有人帮忙。几天来,我一直在努力让美味派工作。
这是我的 api.py
class UserResource(ModelResource):
class Meta:
queryset = User.objects.all()
resource_name = 'user'
fields=['username', 'id']
allowed_methods = ['get']
authorization = Authorization()
authentication = Authentication()
class AdResponsesResource(ModelResource):
users = fields.ManyToManyField('contacts.api.UserResource', 'users',
related_name='adresponse')
class Meta:
queryset = AdResponses.objects.all()
resource_name = 'adresponses'
authorization = Authorization()
authentication = Authentication()
这是我的 ajax 调用
$(function (){
$.ajax({
url: 'url/to/site/contacts/api/v1/adresponses/1',
type: 'GET',
accepts: 'application/json',
dataType: 'json'
});
所以我不完全确定为什么我原来发布的 js 不起作用,但我看到了这个示例 http://django-tastypie.readthedocs.org/en/latest/cookbook.html 并且它按预期工作。可能是因为我没有成功函数?
$.ajax({
url: '../../api/v1/user/',
contentType: 'application/json',
type: 'GET',
success: function(data, textStatus, jqXHR) {
// Your processing of the data here.
console.log(data);
}
});
【问题讨论】:
标签: python django python-2.7 tastypie http-status-code-400