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