I am trying to get a json but I cannot get anything.
This is my code:
<div class="container">
<h1>Ver listas de usuarios</h1>
<form id="getUsers">
<input id="getUsersButton1" type="submit" value="Ver usuarios">
<input id="getUsersButton2" type="button" value="Ver usuarios 2">
<button id="getUsersButton3" onclick="fetchUserData()">Ver usuarios</button>
</form>
</div>
<div id="usersList"></div>
<script>
document.getElementById("getUsersButton1").addEventListener('click', fetchUserData)
document.getElementById("getUsersButton2").addEventListener('click', fetchUserData)
document.getElementById("getUsersButton3").addEventListener('click', fetchUserData)
function fetchUserData() {
fetch('http://localhost:8080/api/users')
.then(function (response) {
return response.json();
})
.then(function (response) {
console.log(response);
});
}
</script>
When I go to read the browser console, I cannot see anything. Thanks guys.
EDIT I changed input type to button and I have this error:
https://i.stack.imgur.com/9zB4P.png
Access to fetch at 'http://localhost:8080/api/users/' from origin 'null' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header has a value 'http://localhost:8081' that is not equal to the supplied origin. Have the server send the header with a valid value, or, if an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
seeUsers.html:32 GET http://localhost:8080/api/users/ net::ERR_FAILED
fetchUserData @ seeUsers.html:32
seeUsers.html:1 Uncaught (in promise) TypeError: Failed to fetch
Promise.then (async)
fetchUserData @ seeUsers.html:36