【发布时间】:2021-07-25 03:59:17
【问题描述】:
我正在使用 fetch API 向服务器发布请求:
function pay() {
fetch('/pay', {
method: 'post',
redirect: 'follow',
body: 'data sent'
})
.then(response => {
// I need to redirect user from front-end here using code below
if(response.redirected) {
window.location.href = response.url;
}
})
.catch(error => console.log(error));
}
然后服务器使用如下重定向响应此请求:
res.redirect(`https://pay.ir/pg/${response.data.token}`);
问题是这个重定向不起作用,我需要从前端重定向用户,就像你在 fetch 函数中看到的那样 window.location.href = response.url;
如何从服务器而不是客户端重定向用户?
【问题讨论】:
标签: javascript node.js fetch-api