I have created a Restful web services from netbeans 8.2 for mysql table users and it gives the following xml output in localhost,
my localhost url : http://localhost:8080/siteuser/webresources/site.users/
This is the output:
<userss>
<users>
<email>abcd@gmail.com</email>
<id>1</id>
<password>81dc9bdb52d04dc20036dbd8313ed055</password>
<userType>user</userType>
<username>new</username>
</users>
<users>
<email>abcd@gmail.com</email>
<id>2</id>
<password>81dc9bdb52d04dc20036dbd8313ed055</password>
<userType>admin</userType>
<username>kamal</username>
</users>
</userss>
I want to show this details in a html table so I wrote a code for it.But it doesn't work. Can you help me to solve this problem? Thank You!
<!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>
When I click the button nothing happens in the browser but, following errors are showing in the browser console.
- Access to XMLHttpRequest at 'http://localhost:8080/siteuser/webresources/site.users/' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
- Failed to load resource: net::ERR_FAILED
- GET http://localhost:8080/siteuser/webresources/site.users/ net::ERR_FAILED loadXMLDoc @ index.html:27 onclick @ index.html:13
@ index.html:27 = xmlhttp.send();
@ index.html:13 = <button type="button" onclick="loadXMLDoc()">Get API data from DB</button>