AJAX - XMLHttpRequest Object
AJAX ၏သော့ချက်မှာ XMLHttpRequest object ဖြစ်သည်။
- XMLHttpRequest object တစ်ခုကို ဖန်တီးပါ။
- ပြန်ခေါ်သည့် လုပ်ဆောင်ချက်ကို သတ်မှတ်ပါ။
- XMLHttpRequest object ကိုဖွင့်ပါ။
- ဆာဗာတစ်ခုထံ တောင်းဆိုချက်တစ်ခု ပေးပို့ပါ။
XMLHttpRequest Object
ခေတ်မီဘရောက်ဆာများအားလုံးသည် XMLHttpRequest
အရာဝတ္ထုကို ပံ့ပိုးပေးသည်။
မြင်ကွင်း နောက်ကွယ် XMLHttpRequest
ရှိ ဝဘ်ဆာဗာတစ်ခုနှင့် ဒေတာဖလှယ်ရန် အရာဝတ္ထုကို အသုံးပြုနိုင်သည်။ ဆိုလိုသည်မှာ စာမျက်နှာတစ်ခုလုံးကို ပြန်မတင်ဘဲ ဝဘ်စာမျက်နှာ၏ အစိတ်အပိုင်းများကို အပ်ဒိတ်လုပ်ရန် ဖြစ်နိုင်သည်။
XMLHttpRequest Object တစ်ခုကို ဖန်တီးပါ။
ခေတ်မီဘရောက်ဆာအားလုံး (Chrome၊ Firefox၊ IE၊ Edge၊ Safari၊ Opera) တွင် built-in XMLHttpRequest
အရာဝတ္ထုတစ်ခုရှိသည်။
XMLHttpRequest
အရာဝတ္ထု တစ်ခုကို ဖန်တီးရန်အတွက် အထားအသို-
variable = new XMLHttpRequest();
Callback Function ကို သတ်မှတ်ပါ။
ပြန်ခေါ်သည့် လုပ်ဆောင်ချက်သည် အခြားလုပ်ဆောင်ချက်တစ်ခုသို့ ကန့်သတ်ချက်တစ်ခုအဖြစ် ပေးပို့သည့် လုပ်ဆောင်ချက်ဖြစ်သည်။
ဤကိစ္စတွင်၊ ပြန်လည်ခေါ်ဆိုမှုလုပ်ဆောင်ချက်တွင် တုံ့ပြန်မှုအဆင်သင့်ဖြစ်သောအခါ လုပ်ဆောင်ရန် ကုဒ်ပါဝင်သင့်သည်။
xhttp.onload = function() {
// What to do when the response is ready
}
တောင်းဆိုချက်တစ်ခုပေးပို့ပါ။
ဆာဗာတစ်ခုထံ တောင်းဆိုချက်တစ်ခုပေးပို့ရန်၊ သင်သည်
XMLHttpRequest
အရာဝတ္ထု၏ open() နှင့် send() နည်းလမ်းများကို အသုံးပြုနိုင်သည်။
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();
Domains များကို ဖြတ်ကျော်ဝင်ရောက်ပါ။
လုံခြုံရေးအရ၊ ခေတ်မီဘရောက်ဆာများသည် ဒိုမိန်းများတစ်လျှောက် ဝင်ရောက်ခွင့်မပြုပါ။
ဆိုလိုသည်မှာ ဝဘ်စာမျက်နှာနှင့် ၎င်းတင်ရန်ကြိုးစားသော XML ဖိုင်နှစ်ခုစလုံးသည် တူညီသောဆာဗာပေါ်တွင် တည်ရှိနေရမည်ဖြစ်သည်။
W3Schools ပေါ်ရှိ နမူနာများသည် W3Schools ဒိုမိန်းတွင်ရှိသော ဖွင့်ထားသော XML ဖိုင်များအားလုံး။
အထက်ဖော်ပြပါ ဥပမာကို သင့်ကိုယ်ပိုင် ဝဘ်စာမျက်နှာတစ်ခုတွင် အသုံးပြုလိုပါက၊ သင်တင်သည့် XML ဖိုင်များသည် သင့်ကိုယ်ပိုင်ဆာဗာပေါ်တွင် တည်ရှိနေရပါမည်။
XMLHttpRequest Object Methods
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 Object Properties
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") |
onload အိမ်ခြံမြေ
အ 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
}
onreadystatechange အိမ်ခြံမြေ
readyState
ပိုင်ဆိုင်မှုသည် XMLHttpRequest ၏ အခြေအနေကို ထိန်းထားသည် ။
ReadyState onreadystatechange
ပြောင်းလဲသောအခါတွင် လုပ်ဆောင်ရန် ပြန်ခေါ်သည့် လုပ်ဆောင်ချက်ကို ပိုင်ဆိုင်မှုက သတ်မှတ်သည်။
status
ပိုင်ဆိုင်မှုနှင့် ဂုဏ်သတ္တိများသည် XMLHttpRequest object ၏ statusText
အခြေအနေကို ထိန်းထားသည်။
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") |
ReadyState onreadystatechange
ပြောင်းလဲသည့်အခါတိုင်း function ကိုခေါ်သည်။
readyState
4 နှင့် status သည် 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();
}
ReadyState onreadystatechange
တွင် ပြောင်းလဲမှုတစ်ခုစီအတွက် တစ်ကြိမ် (1-4) ဖြစ်ရပ်ကို လေးကြိမ် (1-4) အစပျိုးသည်။