我正在尝试使用 jQuery ajax 调用将数值 (ids) 列表从一个网页传递到另一个网页。我不知道如何传递和读取列表中的所有值。我可以成功发布和读取 1 个值,但不能成功发布和读取多个值。这是我到目前为止所拥有的:
jQuery:
var postUrl = "http://localhost:8000/ingredients/";
$('li').click(function(){
values = [1, 2];
$.ajax({
url: postUrl,
type: 'POST',
data: {'terid': values},
traditional: true,
dataType: 'html',
success: function(result){
$('#ingredients').append(result);
}
});
});
/成分/查看:
def ingredients(request):
if request.is_ajax():
ourid = request.POST.get('terid', False)
ingredients = Ingredience.objects.filter(food__id__in=ourid)
t = get_template('ingredients.html')
html = t.render(Context({'ingredients': ingredients,}))
return HttpResponse(html)
else:
html = '<p>This is not ajax</p>'
return HttpResponse(html)
使用 Firebug,我可以看到 POST 包含两个 id,但可能格式错误(terid=1&terid=2)。所以我的成分视图只显示 terid=2。我究竟做错了什么?
EDIT:为了澄清,我需要 ourid 变量将值 [1, 2] 传递到成分视图中的过滤器。
您可以通过以下方式访问该数组request.POST.getlist('terid[]')在视图中
在 JavaScript 中:
$.post(postUrl, {terid: values}, function(response){
alert(response);
});
在 view.py 中:
request.POST.getlist('terid[]')
它非常适合我。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)