[英]How to display values in JS from a PHP array that was posted from jQuery
[英]How to handle a POSTed JS array in Pyramid
我有一個AJAX請求:
var groupName = $('#group-input').val();
var cidArray = [];
$("#sortWrapper .contact-wrapper").children('.is-checked').each(function() {
cidArray.push($(this).attr('cid'));
});
$.ajax({
type: "POST",
url: "/ajax/create/",
data: {group_name: groupName, group_user_id: cidArray},
cache: false,
success: function(result) {
alert(groupName); // These both alert the proper values
alert(cidArray);
}
});
哪個將數據發布到我在Pyramid中的處理程序。 相關部分是:
group_name = request.POST.get('group_name', None)
group_user_id = request.POST.getall('group_user_id')
for index,uid in enumerate(group_user_id):
# perform actions on each user...
print uid
我按預期收到group_name
; 但是,我也很難從JS數組cidArray
接收值。 該數組已按預期填充,但是當我需要使用group_user_id
的鍵檢索這些值時,就會出現問題。
我似乎可以從上面得到的只是一個空列表[]
或使用其他語法時:
request.POST.get('group_user_id', [])
它默認為該空列表。
注意:不用說, for uid in...
中的for uid in...
的前面尚未經過測試或完善,但如果可能的話,我希望group_user_id
的最終結果是最初存在於cidArray
中的那些值的列表。
非常感謝您的幫助。 謝謝。
因此,經過漫不經心的辛勞和痛苦之后,我發現了解決方法:
代替
request.POST.getall('group_user_id') # This is wrong
我需要:
request.POST.getall('group_user_id[]') # This works
getall
檢索一個鍵下存在的所有值,而[]
處理它是一個數組的事實。 其他所有內容保持不變。
希望這可以在某個時候對某人有所幫助。
這會有所幫助嗎? 它使用req.json_body
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.