HTTP POST with post()
In this page:
$.post(url, data, function (response) {
// handle response
}, dataType);
What is post()?
$.post() is a shorthand for making a POST-style AJAX request, most often used to send new data to a server, such as creating a record or submitting a form. Unlike GET, POST data isn't appended to the URL, which suits sending larger or more sensitive payloads.
Example: What is post()?
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<style>
button { padding:6px 14px; margin:4px; background:#2c6aa0; color:white; border:none; border-radius:4px; cursor:pointer; }
#resetBtn { background:white; color:#2c6aa0; border:1px solid #2c6aa0; }
#output { margin-top:10px; padding:8px; border:1px solid #ccc; border-radius:4px; background:#f7f7f7; font-family:monospace; min-height:24px; }
</style>
</head>
<body>
<button id="runBtn">Run</button>
<button id="resetBtn">Reset</button>
<div id="output"></div>
<script>
$(document).ajaxError(function(event, xhr, settings) {
$("#output").append($("<div>").text("Request failed: " + settings.url));
});
$("#runBtn").click(function() {
$("#output").empty();
$("#output").append($("<div>").text("Sending request..."));
$.post("data.php", {name: "Alice"}, function(response) {
$("#output").text(JSON.stringify(response));
});
});
$("#resetBtn").click(function() {
jQuery(document).off();
$("#output").empty();
});
</script>
</body>
</html>
Sending Multiple Values
You can send several values together by passing a plain object as post()'s second argument, and jQuery encodes them into the request body for you. The server then receives them as regular request data, the same as if a form had been submitted normally.
Example: Sending Multiple Values
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<style>
button { padding:6px 14px; margin:4px; background:#2c6aa0; color:white; border:none; border-radius:4px; cursor:pointer; }
#resetBtn { background:white; color:#2c6aa0; border:1px solid #2c6aa0; }
#output { margin-top:10px; padding:8px; border:1px solid #ccc; border-radius:4px; background:#f7f7f7; font-family:monospace; min-height:24px; }
</style>
</head>
<body>
<button id="runBtn">Run</button>
<button id="resetBtn">Reset</button>
<div id="output"></div>
<script>
$(document).ajaxError(function(event, xhr, settings) {
$("#output").append($("<div>").text("Request failed: " + settings.url));
});
$("#runBtn").click(function() {
$("#output").empty();
$("#output").append($("<div>").text("Sending request..."));
$.post("data.php", {name: "Alice", email: "[email protected]"}, function(response) {
$("#output").text(JSON.stringify(response));
});
});
$("#resetBtn").click(function() {
jQuery(document).off();
$("#output").empty();
});
</script>
</body>
</html>
POST Success Callback
After a successful POST request, the callback receives whatever response the server sends back, which you can use to show a confirmation message or update the page with newly created data. This confirms to the user that their submission was actually processed.
Example: POST Success Callback
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<style>
button { padding:6px 14px; margin:4px; background:#2c6aa0; color:white; border:none; border-radius:4px; cursor:pointer; }
#resetBtn { background:white; color:#2c6aa0; border:1px solid #2c6aa0; }
#output { margin-top:10px; padding:8px; border:1px solid #ccc; border-radius:4px; background:#f7f7f7; font-family:monospace; min-height:24px; }
#demo :where(p, div, span, li, h1, h2, h3, h4, td, th, blockquote, label, section, article, aside) { border:1px solid #4a7fb5; background:#eaf2fb; padding:6px 10px; margin:4px 0; border-radius:4px; }
#demo :where(ul, ol) { border:1px dashed #888; background:#fafafa; padding:8px 8px 8px 32px; margin:6px 0; }
#demo :where(table) { border-collapse:collapse; }
#demo :where(td, th) { border-radius:0; }
#demo :where(input, select, textarea) { margin:4px 0; }
</style>
</head>
<body>
<button id="runBtn">Run</button>
<button id="resetBtn">Reset</button>
<section id="demo">
<div id="status"></div>
</section>
<div id="output"></div>
<script>
var original = $("#demo").html();
$(document).ajaxError(function(event, xhr, settings) {
$("#output").append($("<div>").text("Request failed: " + settings.url));
});
$("#runBtn").click(function() {
$("#output").empty();
$("#output").append($("<div>").text("Sending request..."));
$.post("data.php", {name: "Alice"}, function(response) {
$("#status").text("Saved: " + response.message);
});
});
$("#resetBtn").click(function() {
$("#demo").html(original);
jQuery(document).off();
$("#output").empty();
});
</script>
</body>
</html>
POST Error Handling
Chaining .fail() onto a post() call lets you handle a request that doesn't succeed, keeping the user informed instead of leaving a failed submission unacknowledged. This matters more for POST than GET, since a silently-failed POST could mean lost data the user thinks was saved.
Example: POST Error Handling
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<style>
button { padding:6px 14px; margin:4px; background:#2c6aa0; color:white; border:none; border-radius:4px; cursor:pointer; }
#resetBtn { background:white; color:#2c6aa0; border:1px solid #2c6aa0; }
#output { margin-top:10px; padding:8px; border:1px solid #ccc; border-radius:4px; background:#f7f7f7; font-family:monospace; min-height:24px; }
</style>
</head>
<body>
<button id="runBtn">Run</button>
<button id="resetBtn">Reset</button>
<div id="output"></div>
<script>
$("#runBtn").click(function() {
$("#output").empty();
$("#output").append($("<div>").text("Sending request..."));
$.post("data.php", {name: "Alice"}).done(function(response) {
$("#output").append($("<div>").text(JSON.stringify(response)));
}).fail(function() {
$("#output").append($("<div>").text("Submission failed"));
});
});
$("#resetBtn").click(function() {
jQuery(document).off();
$("#output").empty();
});
</script>
</body>
</html>
Small POST Project
POST is useful for sending form data, creating new records, and submitting user input generally -- any case where the request is meant to change something on the server rather than just read from it.
Example: Small POST Project
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<style>
button { padding:6px 14px; margin:4px; background:#2c6aa0; color:white; border:none; border-radius:4px; cursor:pointer; }
#resetBtn { background:white; color:#2c6aa0; border:1px solid #2c6aa0; }
#output { margin-top:10px; padding:8px; border:1px solid #ccc; border-radius:4px; background:#f7f7f7; font-family:monospace; min-height:24px; }
#demo :where(p, div, span, li, h1, h2, h3, h4, td, th, blockquote, label, section, article, aside) { border:1px solid #4a7fb5; background:#eaf2fb; padding:6px 10px; margin:4px 0; border-radius:4px; }
#demo :where(ul, ol) { border:1px dashed #888; background:#fafafa; padding:8px 8px 8px 32px; margin:6px 0; }
#demo :where(table) { border-collapse:collapse; }
#demo :where(td, th) { border-radius:0; }
#demo :where(input, select, textarea) { margin:4px 0; }
</style>
</head>
<body>
<button id="resetBtn">Reset</button>
<section id="demo">
<form id="myForm"><input id="name" value="Alice"></form>
<button id="submitBtn">Save</button>
</section>
<div id="output"></div>
<script>
var original = $("#demo").html();
function init() {
$("#submitBtn").on("click", function() {
$.post("data.php", {name: $("#name").val()}, function(response) {
$("#output").append($("<div>").text("Saved: " + JSON.stringify(response)));
});
});
}
init();
$("#resetBtn").click(function() {
$("#demo").html(original);
$("#output").empty();
init();
});
</script>
</body>
</html>
- Sending data with
$.post("save.php?name=Ann")in the URL, when$.postis meant to send it in the body via the second argument:$.post("save.php", { name: "Ann" }). - Forgetting that the callback only runs on a successful response, so a failed POST needs a chained
.fail(). - Sending the same POST repeatedly through repeated clicks, creating duplicate records, because the button is not disabled while the request runs.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: