【发布时间】:2021-01-25 05:20:01
【问题描述】:
我坚持这一点,我是网络开发的新手,任何帮助将不胜感激。无法弄清楚我的代码有什么问题,我收到一个错误 Uncaught TypeError: response.json is not a function
const getButton = document.querySelector('.get')
function getData(){
const response = fetch("https://reqres.in/api/users");
const data = response.json();
console.log(data)
}
getButton.addEventListener('click',getData)
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>
<button class="get">GET</button>
</div>
<script src="sandbox.js"></script>
</body>
</html>
【问题讨论】:
-
fetch是异步函数。我建议先研究它的文档at MDN。 -
response是Promise。您必须等待它通过await(仅适用于async函数)或.then()完成
标签: javascript json ecmascript-6 async-await fetch