【发布时间】:2021-05-04 17:54:52
【问题描述】:
我有两个文件。
文件 1:
<!DOCTYPE html>
<html>
<body>
<button class="add">Click Here!</button>
<script>
let a;
async function test(file) {
let response = await fetch(file);
return response.text();
}
const bu = document.querySelector(".add");
bu.addEventListener("click", async () => {
a = test("./prova.html");
console.log(a);
});
</script>
</body>
</html>
文件 2:
<h1>Hello, World!</h1>
当我单击按钮时,我想将响应的文本正文从 fetch 传递到 var a。 但我得到的是
index.html:16 Promise {<pending>}
【问题讨论】:
-
response.text()总是返回一个promise,可以根据请求的成功与否来实现或者挂起……你需要获取fetch()请求的数据,这不是与其中 responseText 相同的 XHR 请求相同,请尝试使用 response.data
标签: javascript api fetch