gpt4 book ai didi

javascript - JS 获取 TypeError : Failed To Fetch

转载 作者:数据小太阳 更新时间:2023-10-29 05:35:02 25 4
gpt4 key购买 nike

每当我尝试获取它时,它都会返回一个错误类型错误:无法获取其他解决方案对我不起作用。这是我的代码

fetch(url)
.then((resp) => resp.json())
.then((data) => {
this.JSONData = data;
})
.then(() => {
this.search()
})
.catch(error => {
alert(error);
});

最佳答案

由于异步,页面是否在完成之前重新加载?这可能发生在一些不返回 false 的回调中。

在您的代码中,由于 resp.json() 是异步的,因此 fetch 所在的函数将在 fetch 完成之前返回,但它有足够的时间开始 fetch。例如,如果这是在“onSubmit”中,如果该函数返回一个非假值,则页面会重新加载,从而中断您的提取。当遇到第一个 then(也是异步的)时,它会出错。

修复,

<form onsubmit="myFunctionWithFetch(); return false;">
... or return false in your function and do ...
<form onsubmit="return myFunctionWithFetch();">

关于javascript - JS 获取 TypeError : Failed To Fetch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45228556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com