I use this script to send information to the JsonPlaceholder api. I would like to understand why the following excerpts:
Why should I serialize before using stringify?
var dados = $(this).serialize();
Why in success do I have to put the 'date' inside the function?
success: function(data) {
alert("Sucesso!")
}
Follow the complete code and thank you for the answers!
$(document).ready(function() {
$("#formPost").submit(function() {
var dados = $(this).serialize();
console.log(dados);
$.ajax({
type: 'POST',
url: 'https://jsonplaceholder.typicode.com/posts',
data: JSON.stringify(dados),
success: function(data) {
alert("Sucesso!")
}
});
return false;
});
});