[英]how to get data in 2D array in Django posted by AJAX
我通過 ajax 向 django 發送 2D 列表
這是我的 function 用於發送 ajax 請求
var tid = $(document).find(".table-box a").data("tid")
var newFood = [];
$('#order_table td.food_name').filter(function() {
return $(this).data('status') == 'new';
}).each(function() {
newFood.push( [$(this).text(), $(this).siblings('.quantity').text(), $(this).siblings('.price').text()] );
});
// This will create a 2D array like this [['food_name1','1','$10.00'],['foodname2', '2', '$11.00']]
$.ajax({
type: "POST",
url: $(this).data('url'),
data: {tid:tid, newFood:newFood},
success: function success(data){
alert("Success")
},
});
然后我在 django 中打印request.POST
並得到
<QueryDict: {u'tid': [u'12'], u'newFood[0][]': [u'chick noodle', u'1', u'$2.00'], u'newFood[1][]': [u'Beef Rice', u'1', u'$29.00']}>
當我打印request.POST.get('newFood[0][]')
時,我得到
$2.00
我也試過request.POST.getlist('newFood[0]')
但我得到空列表
[]
有想法該怎么解決這個嗎? 如何一項一項地檢索每個項目?
這就是我的方法。
for i,j in request.POST.items():
print i, request.POST.getlist(i)
而且我可以打印鍵i = newFood [i] []並列出j = ['food','quantity','price']
這就是我解決的方式
for i in range(RowNum):
data = request.GET.getlist(f'newFood[{i}][]')
print (data[0] +' , ' + data[1]+','+data[3])
其中RowNum是2d數組中的行(高度)數。 data [i]是每行(寬度)中的元素。 我希望這會有所幫助
這對我有用,請嘗試單獨的二維數組數據。
for i,j in request.POST.items():
if "newFood" in i:
dtls.append(request.POST.getlist(i))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.