-1
Use this script to send information to the Jsonplaceholder api. I would like to understand why the following snippets:
Why should I serialize before using stringify?
var dados = $(this).serialize();
Because in sucess I must put the 'date' inside the Function?
success: function(data) {
alert("Sucesso!")
}
Follow the full code and thank you for your 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;
});
});
stringify
is not necessary after giving aserialize
in the form, where: The serialize() method creates a text string encoded for URL.– Don't Panic
Thank you and I understood your explanation very well! In the case in question, I am sending the data in Json to an api and tested it without using stringify and it worked! From what I understand, the 'date' method already mounts json automatically?
– cristy
In fact who mounts the response json is the api, the variable
data
receive this json, it would be something like:var data = resuladoApi("https://jsonplaceholder.typicode.com/posts");
This is just an illustrative example– Ricardo Pontual
then the 'date' serves to 'RECEIVE' the answer and not 'SEND' the answer, correct? In the script in question, who assembles the json to send to the api?
– cristy
That line:
var dados = $(this).serialize();
in this case,this
is the form (form
), i.e., data receives data from form fields.– Ricardo Pontual
Thanks brother for the help!
– cristy