JS
var postData = $("#signUpMenu-form :input").serializeArray();
postData = JSON.stringify(postData);
$.ajax({
type: "POST",
url: "/main/class/classregister.php",
data: postData,
cache: false,
processData: false,
contentType: false,
success:function(data){
console.log(data);
console.log("suceess");
},
error: function(jqXHR, textStatus, errorThrown){}
});
PHP
$data = json_decode(stripslashes($_POST['postData']),true);
print_r($data);
我正在尝试使用serializeArray
将表单输入数据发送到php并获取数据,但不知何故我无法将数据返回到ajax。
我的代码出了什么问题?
答案 0 :(得分:-1)
您的ajax配置完全错误,无需对数据进行字符串化
var postData = $("#signUpMenu-form :input").serializeArray();
$.ajax({
type: "POST",
url: "/main/class/classregister.php",
data: postData,
dataType:'json',// adjust according to response type
//cache: false, a POST can't be cached
success: function(data) {
console.log(data);
console.log("suceess");
},
error: function(jqXHR, textStatus, errorThrown) {
}
});
然后在php
$data = $_POST;
echo json_encode($data);