gpt4 book ai didi

javascript - 获取 API,在 useFinalURL 上重新发布 formData

转载 作者:行者123 更新时间:2023-11-30 21:06:10 25 4
gpt4 key购买 nike

有没有办法在 fetch.Response 之后重新发布我的 formData?

我的第一个请求(我向其发送我的 Post 数据)收到 301 代码,这没问题 - 但如果下一个(或下一个)响应是 useFinalURL,我如何重新发送我的 Post-Request?

例子:

fetch(url, {
method: 'post',
body: formData,
redirect: 'follow'
})
.then(function(response) {
if (response.useFinalURL === true){ resendFetchPost }
})

希望有人能给我提示

最佳答案

当您想根据条件检查重做某事时,最简单的解决方案通常是递归函数:

const autoPost = (url, formData) => {
return fetch(url, {
method: 'POST',
body: formData,
redirect: 'follow'
}).then(resp => {
return resp.useFinalURL ?
autoPost(resp.url, formData) : // resend to final url
resp.text(); // could sub resp.json() as approp.
});
};

额外的好处,因为 fetch 是异步的,你不会抛出无限递归错误或无休止地阻塞。

关于javascript - 获取 API,在 useFinalURL 上重新发布 formData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46564339/

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