Ajax函数为什么会返回空数组?

我一直在找解决我问题的方法,但一直没能成功。我基本上是在尝试将数据传递到Ajax函数中,但是当它将数据传递给我的php文件时,它只返回一个空数组(是的,已经有很多类似的问题了,但没有任何我需要的内容)。下面是控制台输出:Array ()

这很奇怪,因为我在Ajax函数之前记录了数据,并且没有问题地打印出了每个部分。POST URL也是准确的,在我的表单中可以很好地工作。我尝试使用响应而不是通过函数传递的数据,但他们也没有用。

这是JS文件:

$(document).ready(function() {
  $('form.ajax').on('submit', function() {

    var that = $(this),
      url = that.attr('action'),
      type = that.attr('method'),
      data = [];

    that.find('[name]').each(function(index, value) {
      var that = $(this),
        name = that.attr('name'),
        value = that.val();

      data[name] = value;
    });
    console.log(data); /////THIS LINE HERE ACTUALLY PRINTS DATA
    $.ajax({
      url: url,
      type: type,
      data: data,
      success: function(data) {
        console.log(data);
      }
    });

    return false;

  });
});

我的PHP:

<?php //removed the issets and other checkers for ease of readability

print_r($_POST);

?>

更新:我尝试将方法:"POST"添加到我的Ajax函数中,它似乎仍在打印空白数组——也许我该把所有东西都改过来?

jQuery ajax() uses GET as default method. You need to mention method: POST for POST requests.

method (default: 'GET')

$.ajax({
   url: url,
   method: "POST",
   type: type,
   data: data,
   success: function(data) {
       console.log(data);
   }
});

Or you can also use post().

EUREKA!!! Wow, the mistake was much simpler than I thought, figured it out solo! Thank you everyone for the tips! Finally got it

$('form.ajax').on('submit', function() {

var that = $(this),
  url = that.attr('action'),
  type = that.attr('method'),
  data = {}; // THIS NEEDS TO BE CHANGED TO BRACKETS!!