AJAX XMLHttpRequest Server Response


Tartalomjegyzék

    Tartalomjegyzék megjelenítése


Szerver válasz tulajdonságai

responseText

a válaszadatokat karakterláncként kapja meg

responseXML

a válaszadatokat XML-adatként kapja meg


A válaszszöveg tulajdonság

A responseText tulajdonság a szerver választ egy JavaScript karakterlánc, és ennek megfelelően használhatja:

Példa

document.getElementById("demo").innerHTML = xhttp.responseText;

Próbálja ki Ön is →

<!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>

A responseXML tulajdonság

Az XMLHttpRequest objektum beépített XML-elemzővel rendelkezik.

A responseXML tulajdonság XML DOM objektumként adja vissza a szerver választ.

Ezzel a tulajdonsággal a választ XML DOM objektumként elemezheti:

Példa

Kérje le a cd_catalog.xml fájlt, és elemezze a választ:

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();

Próbálja ki Ön is →

<!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>


Szerver válaszmódszerek

getResponseHeader()

Adott fejléc-információkat ad vissza a szervererőforrásból

getAllResponseHeaders()

Visszaadja a kiszolgálói erőforrás összes fejlécinformációját


A getAllResponseHeaders() metódus

A getAllResponseHeaders() metódus az összes fejlécinformációt visszaadja a szerver válaszából.

Példa

const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
    document.getElementById("demo").innerHTML =
    this.getAllResponseHeaders();
}
xhttp.open("GET", "ajax_info.txt");
xhttp.send();

Próbálja ki Ön is →

<!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>

A getResponseHeader() metódus

A getResponseHeader() metódus adott fejléc-információkat ad vissza a szerver válaszából.

Példa

const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
    document.getElementById("demo").innerHTML =
    this.getResponseHeader("Last-Modified");
}
xhttp.open("GET", "ajax_info.txt");
xhttp.send();

Próbálja ki Ön is →

<!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>