AJAX - XMLHttpRequest ऑब्जेक्ट
AJAX का कीस्टोन XMLHttpRequest ऑब्जेक्ट है।
- XMLHttpRequest ऑब्जेक्ट बनाएं
- कॉलबैक फ़ंक्शन को परिभाषित करें
- XMLHttpRequest ऑब्जेक्ट खोलें
- सर्वर को एक अनुरोध भेजें
XMLHttpRequest ऑब्जेक्ट
सभी आधुनिक ब्राउज़र XMLHttpRequest
ऑब्जेक्ट का समर्थन करते हैं।
XMLHttpRequest
पर्दे के पीछे वेब सर्वर के साथ डेटा का आदान-प्रदान करने के लिए ऑब्जेक्ट का उपयोग किया जा सकता है । इसका मतलब है कि पूरे पेज को फिर से लोड किए बिना, वेब पेज के कुछ हिस्सों को अपडेट करना संभव है।
एक XMLHttpRequest ऑब्जेक्ट बनाएं
सभी आधुनिक ब्राउज़र (Chrome, Firefox, IE, Edge, Safari, Opera) में एक अंतर्निर्मित XMLHttpRequest
वस्तु होती है।
XMLHttpRequest
ऑब्जेक्ट बनाने के लिए सिंटैक्स :
variable = new XMLHttpRequest();
कॉलबैक फ़ंक्शन को परिभाषित करें
कॉलबैक फ़ंक्शन एक फ़ंक्शन है जिसे किसी अन्य फ़ंक्शन के पैरामीटर के रूप में पारित किया जाता है।
इस मामले में, कॉलबैक फ़ंक्शन में प्रतिक्रिया तैयार होने पर निष्पादित करने के लिए कोड होना चाहिए।
xhttp.onload = function() {
// What to do when the response is ready
}
एक अनुरोध भेजें
सर्वर को अनुरोध भेजने के लिए, आप
XMLHttpRequest
ऑब्जेक्ट के खुले () और भेजें () विधियों का उपयोग कर सकते हैं:
xhttp.open("GET", "ajax_info.txt");
xhttp.send();
उदाहरण
// Create an XMLHttpRequest object
const xhttp = new XMLHttpRequest();
// Define a callback function
xhttp.onload = function() {
// Here you can use the Data
}
// Send a request
xhttp.open("GET", "ajax_info.txt");
xhttp.send();
सभी डोमेन में पहुंचें
सुरक्षा कारणों से, आधुनिक ब्राउज़र सभी डोमेन में एक्सेस की अनुमति नहीं देते हैं।
इसका मतलब यह है कि वेब पेज और एक्सएमएल फाइल दोनों को लोड करने का प्रयास एक ही सर्वर पर होना चाहिए।
W3Schools के उदाहरण W3Schools डोमेन पर स्थित सभी XML फ़ाइलें खोलते हैं।
यदि आप अपने स्वयं के वेब पेजों में से किसी एक पर उपरोक्त उदाहरण का उपयोग करना चाहते हैं, तो आपके द्वारा लोड की जाने वाली XML फ़ाइलें आपके अपने सर्वर पर स्थित होनी चाहिए।
XMLHttpRequest ऑब्जेक्ट मेथड्स
Method | Description |
---|---|
new XMLHttpRequest() | Creates a new XMLHttpRequest object |
abort() | Cancels the current request |
getAllResponseHeaders() | Returns header information |
getResponseHeader() | Returns specific header information |
open(method, url, async, user, psw) | Specifies the request method: the request type GET or POST url: the file location async: true (asynchronous) or false (synchronous) user: optional user name psw: optional password |
send() | Sends the request to the server Used for GET requests |
send(string) | Sends the request to the server. Used for POST requests |
setRequestHeader() | Adds a label/value pair to the header to be sent |
XMLHttpRequest वस्तु गुण
Property | Description |
---|---|
onload | Defines a function to be called when the request is recieved (loaded) |
onreadystatechange | Defines a function to be called when the readyState property changes |
readyState | Holds the status of the XMLHttpRequest. 0: request not initialized 1: server connection established 2: request received 3: processing request 4: request finished and response is ready |
responseText | Returns the response data as a string |
responseXML | Returns the response data as XML data |
status | Returns the status-number of a request 200: "OK" 403: "Forbidden" 404: "Not Found" For a complete list go to the Http Messages Reference |
statusText | Returns the status-text (e.g. "OK" or "Not Found") |
अधिभार संपत्ति
ऑब्जेक्ट के साथ XMLHttpRequest
आप कॉलबैक फ़ंक्शन को निष्पादित करने के लिए परिभाषित कर सकते हैं जब अनुरोध एक उत्तर प्राप्त करता है।
फ़ंक्शन को ऑब्जेक्ट की onload
संपत्ति में परिभाषित किया गया है:XMLHttpRequest
उदाहरण
xhttp.onload = function() {
document.getElementById("demo").innerHTML = this.responseText;
}
xhttp.open("GET", "ajax_info.txt");
xhttp.send();
एकाधिक कॉलबैक फ़ंक्शन
यदि आपके पास वेबसाइट में एक से अधिक AJAX कार्य हैं, तो आपको XMLHttpRequest
ऑब्जेक्ट को निष्पादित करने के लिए एक फ़ंक्शन और प्रत्येक AJAX कार्य के लिए एक कॉलबैक फ़ंक्शन बनाना चाहिए।
फ़ंक्शन कॉल में URL होना चाहिए और प्रतिक्रिया तैयार होने पर कॉल करने के लिए कौन सा फ़ंक्शन होना चाहिए।
उदाहरण
loadDoc("url-1", myFunction1);
loadDoc("url-2", myFunction2);
function loadDoc(url, cFunction) {
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {cFunction(this);}
xhttp.open("GET", url);
xhttp.send();
}
function myFunction1(xhttp) {
// action goes here
}
function myFunction2(xhttp) {
// action goes here
}
ऑनरेडीस्टेटचेंज प्रॉपर्टी
संपत्ति XMLHttpRequest की readyState
स्थिति रखती है।
संपत्ति एक कॉलबैक फ़ंक्शन को onreadystatechange
परिभाषित करती है जिसे रेडीस्टेट में परिवर्तन होने पर निष्पादित किया जाना है।
status
गुण और statusText
गुण XMLHttpRequest ऑब्जेक्ट की स्थिति रखते हैं ।
Property | Description |
---|---|
onreadystatechange | Defines a function to be called when the readyState property changes |
readyState | Holds the status of the XMLHttpRequest. 0: request not initialized 1: server connection established 2: request received 3: processing request 4: request finished and response is ready |
status | 200: "OK" 403: "Forbidden" 404: "Page not found" For a complete list go to the Http Messages Reference |
statusText | Returns the status-text (e.g. "OK" or "Not Found") |
फ़ंक्शन को onreadystatechange
हर बार रेडीस्टेट परिवर्तन कहा जाता है।
कब readyState
4 है और स्थिति 200 है, प्रतिक्रिया तैयार है:
उदाहरण
function loadDoc() {
const xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML =
this.responseText;
}
};
xhttp.open("GET", "ajax_info.txt");
xhttp.send();
}
घटना को onreadystatechange
चार बार (1-4) ट्रिगर किया जाता है, रेडीस्टेट में प्रत्येक परिवर्तन के लिए एक बार।