【发布时间】:2016-10-08 00:02:08
【问题描述】:
当我尝试从 ajax 访问 django 视图发送的数据时,我得到未定义
django 视图:
def GetTenders(request):
print request.POST
if request.method == 'POST':
print request.POST.get('Page')
page_num = int(request.POST.get('Page'))
if page_num != 1:
start = (page_num - 1) * 4
objs = Tender.objects.all()[start:(start + 4)]
return HttpResponse(serializers.serialize('json', objs), content_type='application/json')
else:
objs = Tender.objects.all()[:4]
print serializers.serialize('json', objs)
return HttpResponse(serializers.serialize('json', objs), content_type='application/json')
else:
print 'here'
raise Http404
ajax:
$.ajax({type:"POST",url: "GetTenders",dataType: "json",
data: { "Page": $(this).text() },content_type:'application/json',
success:function (data) {
for(x in data){
alert(x.model);}}})
调试(在浏览器中响应):
[{"model": "Register.tender", "pk": 1, "fields": {"Name": "First", "Kind": "Public Trend", "Category": 1, "Description": "my first bid ", "Created_on": null, "Modified": null, "Active": true, "Size": "S", "Ministry": 1}}]
任何想法:D
【问题讨论】:
-
当你说
debugging (response in browser) :时,你用来打印这个的代码是什么,它在哪里? -
返回的数据是一个数组,所以你可能需要
data[0].model,而不是for ... in循环。 -
@adeneo 也试过了 :(
-
对我来说很好 -> jsfiddle.net/37ad4gdm
-
@ben432rew 来自网络响应这是预览但无法获取数据
标签: javascript jquery ajax django django-models