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!
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.
Sidenote: I think serializeArray()
is much more suitable:
results.push($(this).serializeArray());