Holen Sie sich die Antwortdaten als Zeichenfolge
Erhalten Sie die Antwortdaten als XML-Daten
Die Eigenschaft responseText
gibt die Serverantwort als zurück JavaScript-String, und Sie können ihn entsprechend verwenden:
document.getElementById("demo").innerHTML = xhttp.responseText;
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<div id="demo">
<h2>The XMLHttpRequest Object</h2>
<button type="button" onclick="loadDoc()">Change Content</button>
</div>
<script>
function loadDoc() {
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
document.getElementById("demo").innerHTML =
this.responseText;
}
xhttp.open("GET", "ajax_info.txt");
xhttp.send();
}
</script>
</body>
</html>
Das XMLHttpRequest-Objekt verfügt über einen integrierten XML-Parser.
Die Eigenschaft responseXML
gibt die Serverantwort als XML-DOM-Objekt zurück.
Mit dieser Eigenschaft können Sie die Antwort als XML-DOM-Objekt analysieren:
Fordern Sie die Datei cd_catalog.xml an und analysieren Sie die Antwort:
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();
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h2>The XMLHttpRequest Object</h2>
<p id="demo"></p>
<script>
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
const xmlDoc = this.responseXML;
const x = xmlDoc.getElementsByTagName("ARTIST");
let txt = "";
for (let i = 0; i < x.length; i++) {
txt = txt + x[i].childNodes[0].nodeValue + "<br>";
}
document.getElementById("demo").innerHTML = txt;
}
xhttp.open("GET", "cd_catalog.xml");
xhttp.send();
</script>
</body>
</html>
Gibt spezifische Header-Informationen von der Serverressource zurück
Gibt alle Header-Informationen von der Serverressource zurück
Die Methode getAllResponseHeaders()
gibt alle Header-Informationen aus der Serverantwort zurück.
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
document.getElementById("demo").innerHTML =
this.getAllResponseHeaders();
}
xhttp.open("GET", "ajax_info.txt");
xhttp.send();
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h2>The XMLHttpRequest Object</h2>
<p>The getAllResponseHeaders() function returns all the header information of a resource, like length, server-type, content-type, last-modified, etc:</p>
<p id="demo"></p>
<script>
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
document.getElementById("demo").innerHTML =
this.getAllResponseHeaders();
}
xhttp.open("GET", "ajax_info.txt");
xhttp.send();
</script>
</body>
</html>
Die Methode getResponseHeader()
gibt spezifische Header-Informationen aus der Serverantwort zurück.
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
document.getElementById("demo").innerHTML =
this.getResponseHeader("Last-Modified");
}
xhttp.open("GET", "ajax_info.txt");
xhttp.send();
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h2>The XMLHttpRequest Object</h2>
<p>The getResponseHeader() function is used to return specific header information from a resource, like length, server-type, content-type, last-modified, etc:</p>
<p>Last modified: <span id="demo"></span></p>
<script>
const xhttp=new XMLHttpRequest();
xhttp.onload = function() {
document.getElementById("demo").innerHTML =
this.getResponseHeader("Last-Modified");
}
xhttp.open("GET", "ajax_info.txt");
xhttp.send();
</script>
</body>
</html>