通过ajax提交多种表单 [英] submit multiple forms by ajax
本文介绍了通过ajax提交多种表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过ajax帖子提交多种表单,但是问题是服务器在post中返回了一个空数组.
I'm trying to submit multiple forms thru ajax post, but the problem is the server returns an empty array in post.
这是我的JS中的代码:
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);
}
});
});
在服务器端:
var_dump(json_decode($_POST)); // null
var_dump($_POST); // empty array
我做错了什么?谢谢!
推荐答案
不,没有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
是<form>
标记中使用的属性.
method
is the attribute used in your <form>
tags.
旁注:我认为serializeArray()
更合适:
results.push($(this).serializeArray());
这篇关于通过ajax提交多种表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文