Javascript 如何从API获取xml值
我试图显示API中包含的一些值,这个API被一个叫做PRTG的程序用来从程序中提取信息,这就是API 这个API为您提供了一个如下所示的xml文件Javascript 如何从API获取xml值,javascript,html,ajax,xml,Javascript,Html,Ajax,Xml,我试图显示API中包含的一些值,这个API被一个叫做PRTG的程序用来从程序中提取信息,这就是API 这个API为您提供了一个如下所示的xml文件 <?xml version="1.0" encoding="UTF-8"?> <channels totalcount="0" listend="1"> <prtg-version>16.4.27.6720</prtg-version> <item> <
<?xml version="1.0" encoding="UTF-8"?>
<channels totalcount="0" listend="1">
<prtg-version>16.4.27.6720</prtg-version>
<item>
<name>Tiempo de inactividad</name>
</item>
<item>
<name>Voltaje Bateria</name>
<lastvalue>54,51 VDC</lastvalue>
<lastvalue_raw>0000000000054510.0000</lastvalue_raw>
</item>
</channels>
我想将标签lastvalue中的值显示在网页中,我尝试过使用javascript,但没有任何效果我怀疑您需要javascript,因为结果应该显示在页面上。如果调用API,只需更改ajax部分,一切都应该正常工作。为了模拟API的一些响应,我在文件上做了这些
<html>
<head>
<title>Demo</title>
</head>
<body>
<p id="result"></p>
<script>
var xhttp;
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "channels.xml", true);
xhttp.send();
function myFunction(xml) {
var x, i, txt, xmlDoc, node;
xmlDoc = xml.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName("item");
for (i = 0; i < x.length; i++) {
node = x[i].getElementsByTagName('lastvalue');
if (node && node.length > 0){
txt += node[0].textContent + "<br>";
}
}
document.getElementById("result").innerHTML = txt;
}
</script>
</body>
</html>
这是一个与java、javascript或jquery相关的问题。请说得更清楚一点,欢迎光临。请展示您尝试过的代码,并告诉我们该代码所做的不是您想要的。我尝试过使用javascript,使用requestAjax。我无法添加我尝试过的所有代码,因为代码太长,我是stackoverflow Btwat的新手,至少展示一些您尝试过的代码,这样我们就可以知道这是java还是javascript问题了呵呵。