我已经从netbeans 8.2为mysql表用户创建了Restful Web服务,并在localhost中提供了以下xml输出,
我的本地主机网址:http://localhost:8080/siteuser/webresources/site.users/
这是输出:
<userss>
<users>
<email>[email protected]</email>
<id>1</id>
<password>81dc9bdb52d04dc20036dbd8313ed055</password>
<userType>user</userType>
<username>new</username>
</users>
<users>
<email>[email protected]</email>
<id>2</id>
<password>81dc9bdb52d04dc20036dbd8313ed055</password>
<userType>admin</userType>
<username>kamal</username>
</users>
</userss>
我想在html表中显示此详细信息,所以我为此编写了代码。但这是行不通的。您能帮我解决这个问题吗?谢谢!
<!DOCTYPE html>
<html>
<style>
table,th,td {
border : 1px solid black;
border-collapse: collapse;
}
th,td {
padding: 5px;
}
</style>
<body>
<button type="button" onclick="loadXMLDoc()">Get API data from DB</button>
<br><br>
<table id="demo"></table>
<script>
function loadXMLDoc() {
var url = "http://localhost:8080/siteuser/webresources/site.users/";
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
function myFunction(xml) {
var i;
var xmlDoc = xml.responseXML;
var table="<tr><th></th>Email<th>ID</th><th>Password</th><th>User Type</th><th>User Name</th></tr>";
var x = xmlDoc.getElementsByTagName("users");
for (i = 0; i <x.length; i++) {
table += "<tr><td>" +
x[i].getElementsByTagName("email")[0].childNodes[0].nodeValue +
"</td><td>" +
x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue +
"</td><td>" +
x[i].getElementsByTagName("password")[0].childNodes[0].nodeValue +
"</td><td>" +
x[i].getElementsByTagName("userType")[0].childNodes[0].nodeValue +
"</td><td>" +
x[i].getElementsByTagName("username")[0].childNodes[0].nodeValue +
"</td></tr>";
}
document.getElementById("demo").innerHTML = table;
}
</script>
</body>
</html>
当我单击按钮时,浏览器中没有任何反应,但是浏览器控制台中显示以下错误。
@ index.html:27 = xmlhttp.send();
@ index.html:13 = <button type="button" onclick="loadXMLDoc()">Get API data from DB</button>
根据我的理解。