【发布时间】:2019-10-20 00:58:02
【问题描述】:
我对@987654323@ 世界很陌生,并试图通过使用Fetch API 使用REST API 来运行一个简单的程序。
我的HTML 文件有一个按钮,点击它会点击REST API 并在页面上显示值。
问题是这些值没有显示在页面上。请指导。
home.html
<html>
<head>
<title>Fetch API Demo</title>
</head>
<body>
<h1>Config files</h1>
<button onclick="showUser();">Show user</button>
</body>
<script>
function showUser() {
fetch('https://jsonplaceholder.typicode.com/users/1')
.then(function (response) {
console.log('status: ${response.status}');
console.dir(response.body);
return response.json(); // the important line
})
.then(function (myJson) {
document.write('User: ${myJson.name} <br/> Email: ${myJson.email} <br/> Website: ${myJson.website}');
return myJson;
})
.then(console.log);
}
</script>
</html>
网页(截图):
【问题讨论】:
标签: javascript ajax promise fetch fetch-api