问题描述
我正在尝试获取json,但是我什么也没得到。
这是我的代码:
<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>
当我阅读浏览器控制台时,什么都看不到。 谢谢大家。
编辑 我将输入类型更改为按钮,但出现此错误:
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
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)