我注意到在发送请求后立即更改window.location时,某些获取请求未到达服务器(位置更改不在promise响应处理程序内)。
我在Chrome浏览器上进行了测试
# util.js
function sendLog(payload) {
const urlApi = `http://api.example.com/public/send-message`;
return fetch(urlApi, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
}).then(response => {
return response.json();
});
}
# main.js
sendLog({
project: 'FrontEnd-Card',
countryId: country
}).then(res => {
console.log(res);
});
window.location.replace(REDIRECT_URL);
我注意到了这种模式: 如果网络速度很快,则日志已成功发送到服务器 如果网络很慢,则日志不会到达服务器。
以某种方式重定向会杀死传出的请求。这是真的 ?在哪里可以找到有关此行为的详细文档?