We talked about GET in the last video but didn’t give POST enough attention. So I thought we’d do that here. The big difference with POST is that you nearly always need to pass along data with the request. That data can take many forms, but it might just be an object you have around. The syntax is also easy:
$.post(
"/data/process.php",
{
name: "Susan",
job: "Writer"
}
function(data, textStatus, jqXHR) {
// success
}
);
Notice the second parameter which is an object (essentially JSON) of data. That data can be just about anything (including a string). It’s on you to pass it in such a way that’s useful to your backend.
Back to our form example from the last video, what if you wanted to pass along all the data from the entire form? You also didn’t want to have to update the JavaScript when the form changed. jQuery makes that easy with its serialize() method. Just call it on the form element itself:
$("#my-form").serialize();
That will look through the entire form and make a query string out of each named input in the form. Then you can use that as the data you past when you $.post().
$("#my-form").on("submit", function() {
$.post(
"/",
$("#my-form").serialize(),
function(data, textStatus, jqXHR) {
console.log("success");
}
);
});
See the Pen CtoEg by Chris Coyier (@chriscoyier) on CodePen
Not quite getting this. If the data is sent from a form via the action(=”process.php”) why would I need ajax to do the same thing at the same time?
Would be nice if there was an example with the complete process demonstrated
Because when you hit the submit button you dont want the whole page to reload, you just want the “data sending->request receiving” process to happen silently in the background.
Thats why it is common usage to preventDefault behaviour of the submit button when you do it with ajax (otherwise it would be non-sense as you mentioned)
when you do
serialize()
and that makes it a query string, aren’t query strings limited to like 2000 characters in length?
How would I send a string that is like 40000 characters, would this method work?