AJAX - การตอบกลับของ เซิร์ฟเวอร์
คุณสมบัติการตอบกลับของเซิร์ฟเวอร์
Property | Description |
---|---|
responseText | get the response data as a string |
responseXML | get the response data as XML data |
คุณสมบัติข้อความตอบกลับ
คุณสมบัติresponseText
ส่งคืนการตอบสนองของเซิร์ฟเวอร์เป็นสตริง JavaScript และคุณสามารถใช้ตามนั้น:
ตัวอย่าง
document.getElementById("demo").innerHTML = xhttp.responseText;
การตอบสนองคุณสมบัติ XML
ออบเจ็กต์ XMLHttpRequest มีตัวแยกวิเคราะห์ XML ในตัว
คุณสมบัติresponseXML
ส่งคืนการตอบสนองของเซิร์ฟเวอร์เป็นอ็อบเจ็กต์ XML DOM
การใช้คุณสมบัตินี้ คุณสามารถแยกวิเคราะห์การตอบสนองเป็นวัตถุ XML DOM:
ตัวอย่าง
ขอไฟล์cd_catalog.xmlและแยกวิเคราะห์การตอบสนอง:
const xmlDoc = xhttp.responseXML;
const x = xmlDoc.getElementsByTagName("ARTIST");
let txt = "";
for (let i = 0; i < x.length; i++) {
txt += x[i].childNodes[0].nodeValue + "<br>";
}
document.getElementById("demo").innerHTML = txt;
xhttp.open("GET",
"cd_catalog.xml");
xhttp.send();
วิธีการตอบกลับของเซิร์ฟเวอร์
Method | Description |
---|---|
getResponseHeader() | Returns specific header information from the server resource |
getAllResponseHeaders() | Returns all the header information from the server resource |
เมธอด getAllResponseHeaders()
เมธอดส่งคืนข้อมูล ส่วนgetAllResponseHeaders()
หัวทั้งหมดจากการตอบกลับของเซิร์ฟเวอร์
ตัวอย่าง
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
document.getElementById("demo").innerHTML =
this.getAllResponseHeaders();
}
xhttp.open("GET", "ajax_info.txt");
xhttp.send();
เมธอด getResponseHeader()
เมธอดส่งคืนข้อมูล ส่วนgetResponseHeader()
หัวเฉพาะจากการตอบกลับของเซิร์ฟเวอร์
ตัวอย่าง
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
document.getElementById("demo").innerHTML =
this.getResponseHeader("Last-Modified");
}
xhttp.open("GET", "ajax_info.txt");
xhttp.send();