I have a question regarding posting data with Ajax using either FormData object, serialize() or JSON.stringify() method. When should I use one over the others? IN other words, what are disadvantages and advantages of each? Thank you for your helps.
Using FormData:
var formData = new FormData($("#myFormId")[0]);
$.ajax({
type: "POST",
url: myPostUrl,
data: formData,
async: false,
cache: false,
processData: false,
contentType: false,
success: function (response) {
... }
..});
Using serialize() method:
var data = $("form-input-selectors").serialize();
$.ajax({
type: "POST",
url: myOtherPostUrl,
data: data,
async: false,
cache: false,
success: function (response) {
......
}
});
Using JSON.stringify():
$.ajax({
url: 'myPostUrl',
type: "POST",
cache: false,
datatype: "JSON",
data: JSON.stringify({
"id": id,
"name": name,
"type": type,
"description": description,
"email": email
}),
success: function(result) {
//do stuff
}