通过ajax提交多个表单

now*_*iko 2 javascript php ajax jquery

我试图通过ajax帖子提交多个表单,但问题是服务器在post中返回一个空数组.

以下是我的JS中的代码:

$('#check_test').click(function(e){
    e.preventDefault();
    e.stopPropagation();

    var results = [];
    $('form').each(function(){
        results.push($(this).serialize());
    });

    $.ajax({
        'url': 'handler/test_handler.php',
        'method': 'POST',
        'data': JSON.stringify(results),
        'dataType': 'html',
        'success': function (data) {
            console.log(data);
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

在服务器端:

var_dump(json_decode($_POST)); // null
var_dump($_POST); // empty array
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?谢谢!

Kev*_*vin 6

不,没有method属性,它type:

$.ajax({
    'url': 'handler/test_handler.php',
    'type': 'POST', // type not method
    'data': {data: JSON.stringify(results)},
    'dataType': 'html',
    'success': function (data) {
        console.log(data);
    }
});
Run Code Online (Sandbox Code Playgroud)

method<form>标记中使用的属性.

样本输出

旁注:我认为serializeArray()更合适:

results.push($(this).serializeArray());
Run Code Online (Sandbox Code Playgroud)

另一个例子