weixin_33739541 2015-02-13 08:09 采纳率: 0%
浏览 20

通过ajax提交多种表格

I'm trying to submit multiple forms thru ajax post, but the problem is the server returns an empty array in post.

Here are the codes in my 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!

  • 写回答

1条回答 默认 最新

  • csdnceshi62 2015-02-13 08:11
    关注

    No, there is no method attribute, its 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.

    Sample Output

    Sidenote: I think serializeArray() is much more suitable:

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

    Another example

    评论

报告相同问题?