如何编写 AJAX 调用来获取 XML 文件?

我需要编写一个带有“获取航班信息”按钮的网页。当用户点击这个按钮时,编写一个 AJAX 调用来获取你在问题 1 中编写的 XML 文件,将 XML 解析为一个 Javascript 对象,然后在网页上准确显示该 Javascript 对象,如下所示: 

http://img4.mukewang.com/619f78ce0001058a04970227.jpg

到目前为止,这是我的代码。我对此非常陌生,所以请放轻松!:


XML:


<?xml version="1.0" encoding="UTF-8"?>

<flightInfo>

<Date>02OCT19</Date>

<Flight> VA 1429 </Flight>

<Depart>Sydney</Depart>

<Arrive>Cairns</Arrive>

<Boarding>Gate 35 At 1855</Boarding>

<Carrier>Virgin Australia</Carrier>

</flightInfo>

<?xml version="1.0" encoding="UTF-8"?>

HTML:


<html>

<style>

table,th,td {

  border : 1px solid black;

  border-collapse: collapse;

}

th,td {

  padding: 5px;

}

</style>

<body>


<button type="button" onclick="loadDoc()">Get flight 

information</button>

<br><br>

<table id="demo"></table>


<script>

function loadDoc() {

  var xhttp = new XMLHttpRequest();

  xhttp.onreadystatechange = function() {

    if (this.readyState == 4 && this.status == 200) {

      myFunction(this);

    }

  };

  xhttp.open("GET", "Question1.xml", true);

  xhttp.send();

}

function myFunction(xml) {

  var i;

  var xmlDoc = xml.responseXML;

  var table="<tr><th>Depart</th><th>Arrive</th></tr>";

  var x = xmlDoc.getElementsByTagName("CD");

  for (i = 0; i <x.length; i++) {

    table += "<tr><td>" +

  x[i].getElementsByTagName("Depart")[0].childNodes[0].nodeValue +

    "</td><td>" +

  x[i].getElementsByTagName("Arrive")[0].childNodes[0].nodeValue +

    "</td><td>" +

  x[i].getElementsByTagName("Date")[0].childNodes[0].nodeValue +

    "</td></tr>";

  }

  document.getElementById("demo").innerHTML = table;

}

</script>


</body>

</html>


繁星coding
浏览 141回答 2
2回答

人到中年有点甜

$(function(){&nbsp;var data = '<?xml version="1.0" encoding="UTF-8"?><flightInfo><Date>02OCT19</Date><Flight> VA 1429 </Flight><Depart>Sydney</Depart><Arrive>Cairns</Arrive><Boarding>Gate 35 At 1855</Boarding><Carrier>Virgin Australia</Carrier></flightInfo>'&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;//Parse the givn XMLvar xmlDoc1 = $.parseXML( data );&nbsp;&nbsp; &nbsp;&nbsp;var $xml1 = $(xmlDoc1);&nbsp; // Find Person Tagvar $flightInfo = $xml1.find("flightInfo");$flightInfo.each(function(){&nbsp; &nbsp;&nbsp; &nbsp; var Date = $(this).find('Date').text(),&nbsp; &nbsp; &nbsp; &nbsp; Depart = $(this).find('Depart').text(),&nbsp; &nbsp; &nbsp; &nbsp; Arrive = $(this).find('Arrive').text() ;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $("#data" ).append('<li>' + Date + ' - ' + Depart+&nbsp; ' - ' + Arrive + '</li>');&nbsp; &nbsp;&nbsp;});&nbsp; &nbsp; &nbsp;});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><ul id="data"></ul>首先,您需要在 xml 对象中解析您的数据,如下所示,然后您可以读取它。尝试以下解决方案。<ul id="data"></ul><script>$(function(){&nbsp;var data = '<?xml version="1.0" encoding="UTF-8"?><flightInfo><Date>02OCT19</Date><Flight> VA 1429 </Flight><Depart>Sydney</Depart><Arrive>Cairns</Arrive><Boarding>Gate 35 At 1855</Boarding><Carrier>Virgin Australia</Carrier></flightInfo>'&nbsp; &nbsp;//Parse the givn XMLvar xmlDoc1 = $.parseXML( data );&nbsp;var $xml1 = $(xmlDoc1);&nbsp; // Find Person Tagvar $flightInfo = $xml1.find("flightInfo");$flightInfo.each(function(){&nbsp; &nbsp; var Date = $(this).find('Date').text(),&nbsp; &nbsp; &nbsp; &nbsp; Depart = $(this).find('Depart').text(),&nbsp; &nbsp; &nbsp; &nbsp; Arrive = $(this).find('Arrive').text() ;&nbsp; &nbsp; $("#data" ).append('<li>' + Date + ' - ' + Depart+&nbsp; ' - ' + Arrive + '</li>');});});</script>检查现场演示:http://jsfiddle.net/ru92p4en/

Helenr

<html><style>table,th,td {&nbsp; border : 1px solid black;&nbsp; border-collapse: collapse;}th,td {&nbsp; padding: 5px;}</style><body><button type="button" onclick="loadDoc()">Get flight&nbsp;information</button><br><br><table id="demo"></table><script>function loadDoc() {&nbsp; var xhttp = new XMLHttpRequest();&nbsp; xhttp.onreadystatechange = function() {&nbsp; &nbsp; if (this.readyState == 4 && this.status == 200) {&nbsp; &nbsp; &nbsp; myFunction(this);&nbsp; &nbsp; }&nbsp; };&nbsp; xhttp.open("GET", "Question1.xml", true);&nbsp; xhttp.send();}function myFunction(xml) {&nbsp; var i;&nbsp; var xmlDoc = xml.responseXML;&nbsp; var table=`<tr><th>Depart</th><th>Arrive</th></tr>`;&nbsp; var x = xmlDoc.getElementsByTagName("CD");&nbsp; for (i = 0; i <x.length; i++) {&nbsp; &nbsp; table += `<tr><td>` +&nbsp; x[i].getElementsByTagName("Depart")[0].childNodes[0].nodeValue +&nbsp; &nbsp; `</td><td>` +&nbsp; x[i].getElementsByTagName("Arrive")[0].childNodes[0].nodeValue +&nbsp; &nbsp; `</td><td>` +&nbsp; x[i].getElementsByTagName("Date")[0].childNodes[0].nodeValue +&nbsp; &nbsp; `</td></tr>`;&nbsp; }&nbsp; document.getElementById("demo").innerHTML = table;}</script></body></html>我认为这应该有所帮助。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript