← Back to jQuery Course | Chapter 10: Advanced AJAX | Lesson 1 of 4

ajax() से Low-Level AJAX

ajax() method आपको किसी AJAX request पर detailed control देता है। आप खुद URL, method, data, और callbacks set कर सकते हैं।
Syntax
javascript
$.ajax({
  url: "url",
  type: "GET",
  data: { key: "value" },
  dataType: "json",
  success: function (response) { /* ... */ },
  error: function (xhr, status, error) { /* ... */ }
});

1. ajax() क्या है?

$.ajax() वह low-level method है जिस पर हर jQuery AJAX shortcut ($.get, $.post, $.getJSON) असल में internally बना है। इसे सीधे इस्तेमाल करना हर configuration option पर पूरा control देता है।

उदाहरण: 1. What is ajax()?

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      $.ajax({url: "data.php", success: function(data) {
      console.log(data);
      }});
    </script>
  </body>
</html>

2. GET Requests

GET तब इस्तेमाल करें जब आपको सिर्फ server से data पढ़ना हो, जैसे products की एक list या किसी user की profile fetch करना। Response success callback में आता है।

उदाहरण: 2. GET Requests

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      $.ajax({url: "data.php", method: "GET", success: function(data) {
      console.log(data);
      }});
    </script>
  </body>
</html>

3. POST Requests

POST तब इस्तेमाल करें जब आप server को data भेज रहे हों कुछ create या update करने के लिए, जैसे एक form submit करना या एक comment save करना। data option values रखता है।

उदाहरण: 3. POST Requests

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      $.ajax({url: "data.php", method: "POST", data: {name: "Alice"}, success: function(response) {
      console.log(response);
      }});
    </script>
  </body>
</html>

4. Request Options

ajax() method URL और data के अलावा भी कई configuration options accept करता है। timeout limit करता है कि कितनी देर wait करना है इससे पहले कि give up कर दिया जाए, dataType jQuery को बताता है।

उदाहरण: 4. Request Options

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      $.ajax({
      url: "data.php",
      timeout: 5000,
      dataType: "json",
      success: function(data) { console.log(data); }
      });
    </script>
  </body>
</html>

5. Errors Handle करना

error callback तब fire होता है जब भी request fail हो जाती है — एक network problem, एक timeout, या एक server error status। यह jqXHR object receive करता है, एक status text।

उदाहरण: 5. Handling Errors

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      $.ajax({
      url: "data.php",
      error: function(jqXHR, statusText, errorMsg) {
      console.log(statusText, errorMsg);
      }
      });
    </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. method या type set करना भूल जाना, ताकि request default रूप से GET हो जाए जबकि POST intended था।
  2. data के साथ एक object भेजना लेकिन server JSON की उम्मीद करता है, जब इसके लिए contentType: "application/json" और JSON.stringify(...) चाहिए।
  3. dataType: "json" set करना जब server कुछ और return करता है, जो error callback को trigger कर देता है भले ही request succeed हुई हो।
🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 topics done

Complete these topics first:

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.