使用jQuery通过AJAX POST传递数组

时间:2020-09-17 16:13:55

标签: javascript jquery arrays

我的POST函数根本不传递data[]数组。我的数组正确吗?还是我的POST函数缺少某些内容?

function doAction(prefix) {
  if ($('#' + prefix + '_actions').val() == 'update') {
    var data = [];
    
    for (i = 1; i != 500; i++) {
      if (document.getElementById(prefix + '_region' + i).value == "") {
        $.post('../../wp-content/themes/wine_district/partials/macave.php', {
            client: document.getElementById('currentClient').value,
            'data[]': data,
            action: 'i_update'
          },
          function(result) {
            showData(document.getElementById('currentClient').value);
          });
        break;
      } else {
        data['region' + i] = document.getElementById(prefix + '_region' + i).value;
        data['appellation' + i] = document.getElementById(prefix + '_appellation' + i).value;
        data['domaine' + i] = document.getElementById(prefix + '_domaine' + i).value;
        data['cepage' + i] = document.getElementById(prefix + '_cepage' + i).value;
        data['millesime' + i] = document.getElementById(prefix + '_millesime' + i).value;
        data['format' + i] = document.getElementById(prefix + '_format' + i).value;
        data['nombre' + i] = document.getElementById(prefix + '_nombre' + i).value;
        data['casier' + i] = document.getElementById(prefix + '_casier' + i).value;
        data['caisse' + i] = document.getElementById(prefix + '_caisse' + i).value;
        data['remarque' + i] = document.getElementById(prefix + '_remarque' + i).value;
        data['notes' + i] = document.getElementById(prefix + '_notes' + i).value;
      }
    }
  }
}

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:-3)

您可以通过对数组进行字符串化处理然后传递给帖子调用来解决问题

例如
数据:JSON.stringify(data)