post() से HTTP POST
In this page:
$.post(url, data, function (response) {
// handle response
}, dataType);
post() क्या है?
$.post() एक POST-style AJAX request बनाने का shorthand है, जो अक्सर server को नया data भेजने के लिए इस्तेमाल होता है, जैसे एक record बनाना या एक form submit करना।
उदाहरण: What is post()?
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
<script>
$.post("data.php", {name: "Alice"}, function(response) {
console.log(response);
});
</script>
</body>
</html>
कई Values भेजना
आप post() के दूसरे argument के रूप में एक plain object pass करके कई values एक साथ भेज सकते हैं, और jQuery आपके लिए उन्हें request body में encode कर देता है।
उदाहरण: Sending Multiple Values
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
<script>
$.post("data.php", {name: "Alice", email: "[email protected]"}, function(response) {
console.log(response);
});
</script>
</body>
</html>
POST Success Callback
एक successful POST request के बाद, callback server द्वारा भेजे गए किसी भी response को receive करता है, जिसे आप एक confirmation message दिखाने या page update करने के लिए इस्तेमाल कर सकते हैं।
उदाहरण: POST Success Callback
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
<div id="status"></div>
<script>
$.post("data.php", {name: "Alice"}, function(response) {
$("#status").text("Saved: " + response.message);
});
</script>
</body>
</html>
POST Error Handling
post() call पर .fail() chain करना आपको एक ऐसी request handle करने देता है जो succeed नहीं होती, user को informed रखते हुए बजाय एक failed submission को अनदेखे छोड़ने के।
उदाहरण: POST Error Handling
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
<script>
$.post("data.php", {name: "Alice"}).done(function(response) {
console.log(response);
}).fail(function() {
console.log("Submission failed");
});
</script>
</body>
</html>
एक छोटा POST Project
POST form data भेजने, नए records बनाने, और आम तौर पर user input submit करने के लिए उपयोगी है -- ऐसा कोई भी case जहाँ request का मतलब server-side कुछ बदलना है।
उदाहरण: Small POST Project
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
<form id="myForm"><input id="name" value="Alice"></form>
<button id="submitBtn">Save</button>
<script>
$("#submitBtn").on("click", function() {
$.post("data.php", {name: $("#name").val()}, function(response) {
console.log("Saved:", response);
});
});
</script>
</body>
</html>
$.post("save.php?name=Ann")जैसे URL में data भेजना, जबकि$.postका मतलब इसे दूसरे argument के through body में भेजना है:$.post("save.php", { name: "Ann" })।- यह भूल जाना कि callback सिर्फ एक successful response पर चलता है, इसलिए एक failed POST को एक chained
.fail()चाहिए। - repeated clicks के through बार-बार वही POST भेजना, duplicate records बनाते हुए, क्योंकि request चलने के दौरान button disable नहीं है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: