JS का AJAX
In this page:
const xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.onload = function() {
// xhr.responseText
};
xhr.send();
AJAX का मतलब क्या है
AJAX का मतलब है Asynchronous JavaScript and XML, हालांकि modern AJAX असली XML के बजाय भारी मात्रा में JSON का आदान-प्रदान करता है -- core idea यह है कि server को एक background request की जाए जो पूरे page को reload किए बिना उसका एक हिस्सा update कर दे, वही technique जिसे fetch() एक ज़्यादा modern तरीके से लागू करता है।
उदाहरण: What AJAX Means
// AJAX: background request updates part of a page, no full reload
fetch("data.php")
.then(response => response.json())
.then(data => console.log(data)); // modern AJAX exchanges JSON, not XML
XMLHttpRequest: मूल AJAX API
fetch() के आने से पहले, XMLHttpRequest JavaScript से background HTTP request बनाने का एकमात्र तरीका था -- यह एक XMLHttpRequest object बनाकर, response आने पर एक event listener configure करके, और request असल में भेजने के लिए .send() कॉल करके काम करता है।
उदाहरण: XMLHttpRequest: The Original AJAX API
// Declare the constant `xhr` as a new `XMLHttpRequest` instance
// Declare the constant `xhr` as a new `XMLHttpRequest` instance
const xhr = new XMLHttpRequest();
// Listen for the `load` event on `xhr` and run the handler when it fires
// Listen for the `load` event on `xhr` and run the handler when it fires
xhr.addEventListener("load", () => console.log(xhr.responseText));
// Call `xhr.open("GET", "data.php")`
// Call `xhr.open("GET", "data.php")`
xhr.open("GET", "data.php");
// Call `xhr.send()`
// Call `xhr.send()`
xhr.send();
fetch() ने XMLHttpRequest की जगह काफी हद तक क्यों ली
fetch() एक Promise return करता है, जो .then()/.catch() chains और async/await के साथ स्वाभाविक रूप से जुड़ता है, जबकि XMLHttpRequest एक event callback के अंदर manually readyState जांचने पर निर्भर करता है -- fetch का साफ syntax ही वजह है कि यह नए AJAX-style code के लिए default विकल्प बन गया है।
उदाहरण: Why fetch() Largely Replaced XMLHttpRequest
// Declare the constant `xhr` as a new `XMLHttpRequest` instance
// Declare the constant `xhr` as a new `XMLHttpRequest` instance
const xhr = new XMLHttpRequest();
// Assign `() => { if (xhr.readyState === 4) console.log(xhr.responseText); }` to `xhr.onreadystatechange`
// Assign `() => { if (xhr.readyState === 4) console.log(xhr.responseText); }` to `xhr.onreadystatechange`
xhr.onreadystatechange = () => { if (xhr.readyState === 4) console.log(xhr.responseText); };
// Call `xhr.open("GET", "data.php")`
// Call `xhr.open("GET", "data.php")`
xhr.open("GET", "data.php");
// Call `xhr.send()`
// Call `xhr.send()`
xhr.send();
// Call `fetch("data.php").then(r => r.json()).then(data => console.log(data))`
// Call `fetch("data.php").then(r => r.json()).then(data => console.log(data))`
fetch("data.php").then(r => r.json()).then(data => console.log(data));
Web Application में AJAX कहाँ फिट बैठता है
AJAX-style requests (fetch या XMLHttpRequest के ज़रिए) live search, बिना reload के form submission, infinite-scrolling feeds, real-time dashboard updates, और किसी भी ऐसी interaction को चलाते हैं जहाँ पूरे page को reload करना धीमा या असहज लगेगा जबकि असल में बस एक छोटा data update करना था।
उदाहरण: Where AJAX Fits in a Web Application
// Define the function `search` taking `term`
// Define the function `search` taking `term`
function search(term) {
fetch(`search.php?term=${term}`).then(r => r.json()).then(results => console.log(results));
}
// Call `search("widgets")`
// Call `search("widgets")`
search("widgets");
AJAX और Server-Side Languages
AJAX requests client side पर language-agnostic होती हैं -- JavaScript request को एक जैसे ही तरीके से भेजता है चाहे server को कोई भी चला रहा हो, PHP, Node.js, Python, या कोई और backend -- जब तक server एक ऐसा appropriate response return करता है जिसे JavaScript parse कर सके।
उदाहरण: AJAX and Server-Side Languages
fetch("data.php").then(r => r.json()).then(data => console.log(data)); // could be PHP, Node.js, Python, etc.
- यह मान लेना कि AJAX और XMLHttpRequest obsolete हैं और इन्हें पूरी तरह skip किया जा सकता है -- existing production code का एक significant हिस्सा अभी भी सीधे XMLHttpRequest इस्तेमाल करता है, खासकर पुरानी libraries और frameworks में।
- AJAX (general technique) को XMLHttpRequest (उसे implement करने के लिए एक specific, पुराना API) से confuse करना -- fetch भी AJAX करने का एक तरीका है, बस एक नया।
- आदत से या पुराने tutorials की वजह से नया code XMLHttpRequest से लिखना, जबकि fetch modern browsers में same काम के लिए एक साफ, promise-based API देता है।
- AJAX पूरे page को reload किए बिना background में HTTP request करने की technique को describe करता है -- XMLHttpRequest और fetch दोनों AJAX करने के तरीके हैं।
- XMLHttpRequest AJAX के लिए original browser API है, jo promises के बजाय एक event-based callback style इस्तेमाल करता है।
- नए code के लिए, fetch() को आमतौर पर XMLHttpRequest से ज़्यादा prefer किया जाता है उसके साफ, promise-based syntax के लिए -- लेकिन existing code पढ़ने के लिए XMLHttpRequest समझना अभी भी उपयोगी है।
XMLHttpRequest को 2000s की शुरुआत से हर browser में support मिला है; fetch modern replacement की तरह सभी current browsers में supported है।
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS JSON
- JS Regular Expressions
- JS Fetch API
- JS LocalStorage and SessionStorage
- JS Cookies
- JS setTimeout and setInterval
- JS Event Loop
- JS Web Workers
- JS Service Workers
- JS AJAX
- JS AJAX Intro
- JS AJAX XMLHttp
- JS AJAX Request
- JS AJAX Response
- JS AJAX XML
- JS AJAX PHP
- JS AJAX Database
- JS JSONP
- JS RegExp Flags
- JS RegExp Classes
- JS RegExp Metachars
- JS RegExp Assertions
- JS RegExp Groups
- JS RegExp Quantifiers
- JS JSON HTML
- JS JSON vs XML