在 useFinalURL 上获取 API、重新 Post formData
fetch API, re-Post formData on useFinalURL
有没有办法在 fetch.Response 之后重新 Post 我的 formData 是重定向了?
我的第一个请求(为了发送我的 Post 数据)得到一个 301 代码,这没问题 - 但如果下一个(或下一个下一个)我如何重新发送我的 Post 请求) 响应是useFinalURL?
示例:
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
是异步的,你不会抛出无限递归错误或无休止地阻塞。
有没有办法在 fetch.Response 之后重新 Post 我的 formData 是重定向了?
我的第一个请求(为了发送我的 Post 数据)得到一个 301 代码,这没问题 - 但如果下一个(或下一个下一个)我如何重新发送我的 Post 请求) 响应是useFinalURL?
示例:
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
是异步的,你不会抛出无限递归错误或无休止地阻塞。