我是学习Javascipt的初学者。我想要做的是使用javascript代码从Koha库系统获取数据。 Koha使用Apache2 Web服务器和MySQL数据库来存储目录。
我的代码是:
<!DOCTYPE html>
<html>
<body>
<div id="demo"><h2>Result</h2></div>
<script>
var xhttp = new XMLHttpRequest();
xhttp.open("GET","http://127.0.0.1:8080/cgi-bin/koha/svc/bib/3?userid=user&password=user1", false);
xhttp.send();
xmlDoc = xhttp.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName("a");
for (i = 0; i < x.length; i++) {
txt += x[i].childNodes[0].nodeValue + "<br>";
}
document.getElementById("demo").innerHTML = txt;
</script>
</body>
</html>
它不起作用。
当我输入网址时:
http://127.0.0.1:8080/cgi-bin/koha/svc/bib/3?userid=user&password=user1
在我的浏览器中我得到了这个
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.loc.gov/MARC21/slim" xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd">
<leader>00099nam a22000617a 4500</leader>
<datafield tag="020" ind1=" " ind2=" ">
<subfield code="a">234234</subfield>
</datafield>
<datafield tag="245" ind1=" " ind2=" ">
<subfield code="a">Harry Potter</subfield>
</datafield>
<datafield tag="999" ind1=" " ind2=" ">
<subfield code="c">3</subfield>
<subfield code="d">3</subfield>
</datafield>
</record>
我想使用javascript获取输出。请帮忙!
答案 0 :(得分:0)
如果jQuery
是一个选项,我建议将它用于您的ajax调用和XML处理。我将您的xml结果作为文件保存在本地作为“Temp.xml”,并在本地Web服务器上创建了下面的页面。 $.get("Temp.xml")
调用是一个ajax调用,它将文件作为XML文档提取到内存中。从那里,我使用jQuery来查找我想要的节点并将它们写入页面。 (document.write
可能不是获取页面数据的最佳方式,但它可以完成工作以进行演示。)
<!DOCTYPE html>
<html>
<body>
<div id="demo"><h2>Result</h2></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.js"></script>
<script>
// Get the xml via a "GET" request. When it's done, process the results.
$.get("Temp.xml").done(function(xmlDoc) {
// The resulting xml is basically a bunch of datafield nodes that contain
// subfield nodes. Write each datafield values and then each of their
// subfield values.
$(xmlDoc).find("datafield").each(function(index, datafield) {
// getAttribute can be used on native elements to get their attribute values...
document.write('datafield[tag] = ' + datafield.getAttribute('tag'));
document.write('<br>');
// $(...) wraps an element with jQuery functionality and allows you quickly
// and easily find / process particular notes.
$(datafield).find("subfield").each(function(index, subfield) {
document.write(' '); // for indentation
document.write('subfield[code] = ' + subfield.getAttribute('code'));
document.write(', '); // for indentation
document.write('subfield = ' + subfield.textContent);
document.write('<br>');
});
});
});
</script>
</body>
</html>