【发布时间】:2012-06-25 22:57:17
【问题描述】:
我正在尝试使用 jQuery ajax 调用将数值 (id) 列表从一个网页传递到另一个网页。我不知道如何传递和读取列表中的所有值。我可以成功发布和读取 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);
}
});
});
/ingredients/ 视图:
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。我做错了什么?
编辑: 澄清一下,我需要将 ourid 变量传递值 [1, 2] 传递给成分视图中的过滤器。
【问题讨论】: