简体   繁体   English

通过ajax提交多种表格

[英]submit multiple forms by ajax

I'm trying to submit multiple forms thru ajax post, but the problem is the server returns an empty array in post. 我正在尝试通过ajax帖子提交多种表单,但是问题是服务器在帖子中返回了一个空数组。

Here are the codes in my JS: 这是我的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);
        }
    });
});

In server side: 在服务器端:

var_dump(json_decode($_POST)); // null
var_dump($_POST); // empty array

What am I doing wrong? 我究竟做错了什么? Thanks! 谢谢!

No, there is no method attribute, its type : 不,没有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);
    }
});

method is the attribute used in your <form> tags. method<form>标记中使用的属性。

Sample Output 样本输出

Sidenote: I think serializeArray() is much more suitable: 旁注:我认为serializeArray()更合适:

results.push($(this).serializeArray());

Another example 另一个例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM