// Send AJAX request to route
function fetch_post(key, ...values){
const formData = new FormData();
for (let value of values){
formData.append(key, value)
}
fetch('/settings', {
method: 'post',
body: formData
});
}
在这个属性值中:
onchange="fetch_post(this.name, this.value); location.reload();"
我想在fetch_post
运行结束时运行location.reload()
。
您希望location.reload()在fetch_post运行完成时运行。
then
为承诺链。
如果fetch被成功调用,代码将被执行。
fetch('/settings', {
method: 'post',
body: formData
})
.then((res) => {
console.log(res);
location.reload();
});
只需在location.reload()中添加这样的fetch函数:
// Send AJAX request to route
function fetch_post(key, ...values){
const formData = new FormData();
for (let value of values){
formData.append(key, value)
}
fetch('/settings', {
method: 'post',
body: formData
}).then((data) => {
location.reload(); <----- this
}).catch((err)=> {
}).finally(() => {
});
}
希望这有帮助!