JS AJAX रिस्पॉन्स
In this page:
xhr.responseText
xhr.responseXML
xhr.status
xhr.getResponseHeader("name");
एक JSON Response पढ़ना
response.json() response body को JSON के रूप में parse करता है और parsed JavaScript value में resolve होने वाला एक Promise return करता है -- structured API responses पढ़ने का सबसे आम तरीका, क्योंकि ज़्यादातर modern APIs डिफ़ॉल्ट रूप से JSON return करते हैं।
उदाहरण: Reading a JSON Response
// Define the method `fetch` taking `"data.php"`
// Define the method `fetch` taking `"data.php"`
fetch("data.php")
// On success, run this with the resolved value as `response`
// On success, run this with the resolved value as `response`
.then(response => response.json())
// On success, run this with the resolved value as `data`
// On success, run this with the resolved value as `data`
.then(data => console.log(data));
response.ok और Status जांचना
response.ok एक boolean है, जो सिर्फ तभी true होता है जब HTTP status 200-299 range में हो -- इसे (या सीधे response.status को) जांचना ज़रूरी है, क्योंकि fetch का promise एक 404 या 500 response के लिए भी सामान्य रूप से resolve होता है, किसी failed request से आपकी उम्मीद के उलट।
उदाहरण: Checking response.ok and Status
fetch("data.php").then(response => {
console.log(response.ok, response.status); // check before trusting the data
return response.json();
});
Text और दूसरे Response Formats पढ़ना
response.text() body को एक plain string के रूप में पढ़ता है, जो HTML fragments या plain-text responses के लिए उपयोगी है, जबकि response.blob() binary data (जैसे कोई image या file) पढ़ता है, हर एक एक Promise return करता है, ठीक response.json() की तरह।
उदाहरण: Reading Text and Other Response Formats
fetch("data.php").then(response => response.text()).then(text => console.log(text));
Response Headers पढ़ना
response.headers.get(headerName) response से एक खास header पढ़ता है, जो server द्वारा body के साथ भेजे गए metadata के लिए उपयोगी है -- जैसे format confirm करने के लिए Content-Type, या यह बताने वाले custom pagination headers कि कुल कितने results मौजूद हैं।
उदाहरण: Reading Response Headers
fetch("data.php").then(response => {
// Print `response.headers.get("Content-Type")` to the console
// Print `response.headers.get("Content-Type")` to the console
console.log(response.headers.get("Content-Type"));
});
Response Body सिर्फ एक बार पढ़ी जा सकती है
किसी response की body एक stream है जिसे सिर्फ एक बार consume किया जा सकता है -- एक ही response object पर response.json() और फिर response.text() कॉल करना दूसरी call पर fail हो जाता है, क्योंकि body पहले ही पढ़ी और drain की जा चुकी है।
उदाहरण: The Response Body Can Only Be Read Once
fetch("data.php").then(response => {
return response.json().then(data => {
console.log(data);
// return response.text(); // would fail, body already read
});
});
- किसी ऐसे response पर response.json() कॉल करना जो असल में JSON नहीं है (जैसे एक HTML error page), जो अनपेक्षित content return करने के बजाय एक parsing error throw करता है।
- यह भूल जाना कि fetch() का promise सिर्फ network failure पर reject होता है, किसी HTTP error status पर नहीं -- किसी 404 या 500 का पता लगाने के लिए response.ok को explicitly check करना ज़रूरी है।
- किसी fetch response body को एक से ज़्यादा बार पढ़ने की कोशिश करना -- body stream को हर response object के लिए सिर्फ एक बार consume किया जा सकता है।
- response.json(), response.text(), और response.blob() हर एक response body को अलग format में parse करते हैं, और हर एक एक Promise return करता है।
- response.ok सिर्फ HTTP status codes 200-299 के लिए true होता है -- किसी response को successful मानने से पहले हमेशा इसे check करें।
- response.headers.get(name) एक specific response header पढ़ता है, content type या pagination metadata के लिए उपयोगी।
fetch और XMLHttpRequest responses पढ़ना हर modern browser में समान रूप से काम करता है।
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