AJAX XMLHttpRequest การตอบสนองของเซิร์ฟเวอร์


สารบัญ

    แสดงสารบัญ


คุณสมบัติการตอบสนองของเซิร์ฟเวอร์

responseText

รับข้อมูลการตอบสนองเป็นสตริง

responseXML

รับข้อมูลการตอบสนองเป็นข้อมูล XML


คุณสมบัติข้อความตอบกลับ

responseText คุณสมบัติส่งกลับการตอบสนองของเซิร์ฟเวอร์เป็น สตริง JavaScript และคุณสามารถใช้ตามนั้นได้:

ตัวอย่าง

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

ลองด้วยตัวคุณเอง →

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

คุณสมบัติ responseXML

วัตถุ 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();

ลองด้วยตัวคุณเอง →

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


วิธีการตอบกลับของเซิร์ฟเวอร์

getResponseHeader()

ส่งกลับข้อมูลส่วนหัวเฉพาะจากทรัพยากรเซิร์ฟเวอร์

getAllResponseHeaders()

ส่งกลับข้อมูลส่วนหัวทั้งหมดจากทรัพยากรเซิร์ฟเวอร์


เมธอด getAllResponseHeaders()

getAllResponseHeaders() วิธีการส่งคืนข้อมูลส่วนหัวทั้งหมดจากการตอบกลับของเซิร์ฟเวอร์

ตัวอย่าง

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

ลองด้วยตัวคุณเอง →

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

เมธอด 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();

ลองด้วยตัวคุณเอง →

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