javascript - Ajax 不发送数据作为参数

标签 javascript jquery 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");
      }
    });
  });
});
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title></title>
  <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="app.js"></script>
</head>

<body>
  <table id="table" class="tablesorter" width="100%" cellspacing="0">
    <thead>
      <tr>
        <th>H1</th>
        <th>H2</th>
        <th>H3</th>
        <th>H4</th>
        <th>H5</th>
        <th></th>
        <th>H6</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>1</td>
        <td>1</td>
        <td>11002WR</td>
        <td>201100</td>
        <td>A</td>
        <td class="center"><input class="11002WR" id="box_201100" type="checkbox" /></td>
        <td id="td_201100">$320.00</td>
      </tr>
      <tr>
        <td colspan="3"></td>
        <td>Check. No</td>
        <td><input id="txt_11002WR" type="text"></td>
        <td><input id="box_total_11002WR" class="box_total_201100" type="checkbox" /></td>
        <td id="sub_11002WR">$12.00</td>
    </tbody>
  </table>
  <input id="btn" type="button" value="Test" />
</body>

</html>

请选中两个复选框和输入内容,然后按测试。

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

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

谢谢。

最佳答案

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

var allChecks = [];

var allChecks = {};

当您使用 $.ajax 发送数组时,它仅发送具有数字索引的元素,而不发送命名属性。

关于javascript - Ajax 不发送数据作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47999292/

相关文章:

javascript - jQuery AJAX make 命令呈现 404 结果

java - Hibernate 连接两个表

javascript - 单击动态创建的按钮和 AJAX 调用替换内容

javascript - 我想使用复选框来改变 var 的输出

javascript - 单例比 TypeScript 中的模块有优势吗?

javascript - 从 Node 确定Docker for Mac RAM限制

javascript - preventDefault() 和单选按钮的奇怪行为

javascript - Ajax Mysql 查询

jQuery - 留在页面底部

javascript - 如何将 JavaScript 相同的函数变成可重用的函数?