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

Cross-Domain Requests: JSONP और CORS

एक browser आमतौर पर अलग-अलग origins के बीच requests को restrict करता है। CORS किसी server के लिए cross-origin requests allow करने का standard तरीका है।
Syntax
javascript
$.ajax({
  url: "url",
  dataType: "jsonp",
  success: function (data) { /* ... */ }
});

1. Cross-Domain AJAX क्या है?

एक cross-domain AJAX request उस page से अलग origin — एक अलग domain, protocol, या port — को target करती है जिसने इसे बनाया, और browsers इसे restrict करते हैं।

उदाहरण: 1. What Is Cross-Domain AJAX?

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

2. CORS की Basics

CORS (Cross-Origin Resource Sharing) पूरी तरह से server द्वारा HTTP response headers जैसे Access-Control-Allow-Origin के through control होता है; jQuery नहीं कर सकता।

उदाहरण: 2. CORS Basics

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      // CORS is controlled by the server's response headers, not by jQuery
      $.ajax({
      url: "https://api.example.com/data",
      success: function(data) { console.log(data); }
      });
    </script>
  </body>
</html>

3. jQuery के साथ JSONP

JSONP cross-domain GET requests बनाने की एक पुरानी technique है जो CORS से पहले की है, जो एक real AJAX request के बजाय एक script tag load करके काम करती है।

उदाहरण: 3. JSONP with jQuery

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

4. Credentials के साथ CORS

Credentialed CORS requests किसी cross-origin call के साथ cookies या दूसरे credentials भेज सकती हैं, लेकिन सिर्फ तभी जब client withCredentials set करे और।

उदाहरण: 4. CORS with Credentials

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

5. CORS बनाम JSONP

CORS cross-origin AJAX के लिए modern, standard choice है और सभी HTTP methods support करता है, जबकि JSONP सिर्फ GET requests तक limited है और सिर्फ तभी काम करता है जब।

उदाहरण: 5. CORS vs JSONP

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      // CORS: modern, all HTTP methods. JSONP: legacy, GET only, needs server support
      $.ajax({url: "https://api.example.com/data", dataType: "json"});
    </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. यह मान लेना कि किसी दूसरे domain को $.ajax() बस काम कर जाएगा, जबकि browser response को block कर देता है जब तक server Access-Control-Allow-Origin न भेजे।
  2. method: "POST" के साथ JSONP try करना, जबकि JSONP सिर्फ GET के साथ काम करता है क्योंकि यह एक <script> tag load करता है।
  3. ऐसे server के लिए dataType: "jsonp" set करना जो अपना response callback function में wrap नहीं करता, ताकि callback कभी न चले।
चैप्टर सारांश
  • ajax() वह low-level method है जो requests पर पूरा control देता है।
  • Headers और status codes को process किया जा सकता है, और global hooks सारी AJAX activity सुनते हैं।
  • Cross-domain requests JSONP और CORS इस्तेमाल करती हैं।
🔒

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.