AJAX - Phản hồi của máy chủ
Thuộc tính phản hồi của máy chủ
Property | Description |
---|---|
responseText | get the response data as a string |
responseXML | get the response data as XML data |
Thuộc tính responseText
Thuộc responseText
tính trả về phản hồi máy chủ dưới dạng một chuỗi JavaScript và bạn có thể sử dụng nó cho phù hợp:
Thí dụ
document.getElementById("demo").innerHTML = xhttp.responseText;
Thuộc tính responseXML
Đối tượng XMLHttpRequest có trình phân tích cú pháp XML được tích hợp sẵn.
Thuộc responseXML
tính trả về phản hồi máy chủ dưới dạng đối tượng DOM XML.
Sử dụng thuộc tính này, bạn có thể phân tích cú pháp phản hồi dưới dạng đối tượng DOM XML:
Thí dụ
Yêu cầu tệp cd_catalog.xml và phân tích cú pháp phản hồi:
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();
Phương thức phản hồi của máy chủ
Method | Description |
---|---|
getResponseHeader() | Returns specific header information from the server resource |
getAllResponseHeaders() | Returns all the header information from the server resource |
Phương thức getAllResponseHeaders ()
Phương getAllResponseHeaders()
thức trả về tất cả thông tin tiêu đề từ phản hồi của máy chủ.
Thí dụ
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
document.getElementById("demo").innerHTML =
this.getAllResponseHeaders();
}
xhttp.open("GET", "ajax_info.txt");
xhttp.send();
Phương thức getResponseHeader ()
Phương getResponseHeader()
thức trả về thông tin tiêu đề cụ thể từ phản hồi của máy chủ.
Thí dụ
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
document.getElementById("demo").innerHTML =
this.getResponseHeader("Last-Modified");
}
xhttp.open("GET", "ajax_info.txt");
xhttp.send();