Ajax不将数据作为参数发送

我正在尝试通过ajax发送一个数组,所以我有以下代码:

这是代码

function fncGetChecksToProcess() { var allChecks = []; $('input[type=text]').each(function() { var key = $(this).attr("id").replace("txt_", ""); allChecks[key] = []; }); $('input[type=checkbox]').each(function() { if (this.checked) { var className = $(this).attr('class'); if (className.includes('box_total_')) { var ref = $(this).attr('id').replace("box_total_", ""); var amountDetails = []; var docNs = []; $('.' + ref).each(function() { amountDetails.push(parseFloat($(this).closest('td').next().html().replace("$", "").replace(",", ""))); docNs.push($(this).attr('id').replace("box_", "")); }); allChecks[ref].push({ docN: docNs, amountTot: $("#sub_" + ref).text(), amountDetails: amountDetails, chkNum: $("#txt_" + ref).val() }); } else { var docN = $(this).attr('id').replace("box_", ""); allChecks[className].push({ docN: docN, amountTot: $("#td_" + docN).text(), amountDetails: "", chkNum: $("#txt_" + className).val() }); } } }); return allChecks; } $(document).ready(function() { $("#btn").click(function() { var checks = fncGetChecksToProcess(); console.log(checks); $.ajax({ cache: false, type: 'POST', data: { allChecks: checks }, url: '/process', beforeSend: function() { console.log("Processing your checks please wait..."); }, success: function(response) { console.log(response); }, error: function() { console.log("Error"); } }); }); }); 
          
H1 H2 H3 H4 H5 H6
1 1 11002WR 201100 A $320.00
Check. No $12.00

请检查两个复选框和输入,然后按下测试。

控制台打印出生成的数组,但Ajax不发送它。

为什么我的ajax调用不发送任何参数? 我没有在chrome控制台中看到它们。

谢谢。

由于您的键是字符串而不是数字,因此您应该使用对象,而不是数组。 更改

 var allChecks = []; 

 var allChecks = {}; 

使用$.ajax发送数组时,它只发送带有数字索引的元素,而不是命名属性。

你需要发送一个数组到ajax,现在你在数组中发送一个数组数组…如果你像服务器一样发布到服务器,post将如下所示:

 allchecks[] = x allchecks[] = y ... 

https://plnkr.co/edit/SnZmKgRLODYb8p6kPryn?p=preview

  $.ajax({ cache: false, type: 'POST', data: { allChecks: checks["11002WR"][0] }, 

这是一个快速的解决方案。 但这不是一个好习惯

而不是allchecks = []。 尝试用键或id构建对象,而不是像ref那样的字符串作为数组的键

对象如下:

  var allChecks = {};