babayaga babayaga - 1 month ago 14
jQuery Question

serializeArray can't get error

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);


I'm trying to use
serializeArray
to send my form input data to php and get the data back, but somehow I can't get the data back at ajax.

What's wrong with my code?

Answer

Your ajax config is all wrong and there is no need to stringify the data

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) {

  }
});

Then in php

$data = $_POST;

echo json_encode($data);
Comments