← Back to jQuery Course | Chapter 9: AJAX Basics | Lesson 4 of 7

post() से HTTP POST

post method server को data भेजता है। यह आमतौर पर कुछ create या submit करते समय इस्तेमाल होता है।
Syntax
javascript
$.post(url, data, function (response) {
  // handle response
}, dataType);

post() क्या है?

$.post() एक POST-style AJAX request बनाने का shorthand है, जो अक्सर server को नया data भेजने के लिए इस्तेमाल होता है, जैसे एक record बनाना या एक form submit करना।

उदाहरण: What is post()?

javascript
<!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

javascript
<!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

javascript
<!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

javascript
<!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

javascript
<!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>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. $.post("save.php?name=Ann") जैसे URL में data भेजना, जबकि $.post का मतलब इसे दूसरे argument के through body में भेजना है: $.post("save.php", { name: "Ann" })।
  2. यह भूल जाना कि callback सिर्फ एक successful response पर चलता है, इसलिए एक failed POST को एक chained .fail() चाहिए।
  3. repeated clicks के through बार-बार वही POST भेजना, duplicate records बनाते हुए, क्योंकि request चलने के दौरान button disable नहीं है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.