Headers और Status Codes Process करना
In this page:
$.ajax({
url: "url",
statusCode: {
404: function () { /* not found */ }
}
});
1. Response Headers पढ़ना
एक AJAX call से return किया गया jqXHR object आपको server द्वारा भेजे गए raw HTTP response headers inspect करने देता है, जैसे Content-Type या कोई custom API header।
उदाहरण: 1. Reading Response Headers
<!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, status, jqXHR) {
console.log(jqXHR.getResponseHeader("Content-Type"));
}});
</script>
</body>
</html>
2. Status Codes Check करना
HTTP status codes आपको बताते हैं कि request का असल में क्या हुआ, इससे independent कि jQuery का success या error callback fire हुआ या नहीं। एक 200 response आमतौर पर।
उदाहरण: 2. Checking Status Codes
<!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, status, jqXHR) {
console.log("Status code:", jqXHR.status);
}});
</script>
</body>
</html>
3. beforeSend और Request Headers
beforeSend इस्तेमाल करें किसी request को भेजे जाने से ठीक पहले inspect या modify करने के लिए, जो एक authentication token जैसी चीज़ attach करने की standard जगह है।
उदाहरण: 3. beforeSend and Request Headers
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
<script>
$.ajax({
url: "data.php",
beforeSend: function(jqXHR) {
jqXHR.setRequestHeader("Authorization", "Bearer token123");
}
});
</script>
</body>
</html>
4. statusCode Option
statusCode option आपको specific HTTP status codes के लिए अलग handler functions define करने देता है, जैसे 404 या 403, एक बड़ा error handler लिखने के बजाय।
उदाहरण: 4. statusCode Option
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
<script>
$.ajax({
url: "data.php",
statusCode: {
404: function() { console.log("Not found"); },
403: function() { console.log("Forbidden"); }
}
});
</script>
</body>
</html>
5. Headers और Status को Combine करना
आप headers और status codes को साथ inspect करके request का fuller picture बना सकते हैं — उदाहरण के लिए, status code और दोनों को log करना।
उदाहरण: 5. Combining Headers and Status
<!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, status, jqXHR) {
console.log("Status:", jqXHR.status, "Type:", jqXHR.getResponseHeader("Content-Type"));
}});
</script>
</body>
</html>
- एक cross-domain request पर
xhr.getResponseHeader("x-token")से headers पढ़ना औरnullमिलना, क्योंकि server को उन्हेंAccess-Control-Expose-Headersमें list करना ज़रूरी है। - 404 या 500 जैसे non-200 status को success मानना, जबकि jQuery उनके लिए
errorcallback call करता है और code कोjqXHR.statuscheck करना चाहिए। - request भेजे जाने के बाद request headers set करना, जबकि
beforeSend(याheadersoption) वह जगह है जहाँ उन्हें set किया जाना चाहिए।
Chapter Quiz — Complete all 4 topics to unlock
0/4 topics done
Complete these topics first: