React:如何成功提交表单数据,然后刷新页面/重定向到主页



我正在提交数据。阿皮工作得很好如果我不使用window.locationhref=",它会提交数据/&">请更新我的代码,成功地一直插入数据,然后页面刷新重定向到主页。请帮忙。

handleSubmit(event) {
event.preventDefault();
const Category_ID = this.props.key_id;
const CategoryName = this.state.CategoryName;
const data = {
Category_ID,
CategoryName
}
fetch(REQUEST_URL, {
method: 'POST',
body: JSON.stringify(data),
headers: { 'Content-Type': 'application/json' }
})
.then(response => response.json())
.catch(() => console.log("error in posting data"));
window.location.href = "/";
}
}

感谢

只需在then()中添加刷新部分

handleSubmit(event) {
event.preventDefault();
const Category_ID = this.props.key_id;
const CategoryName = this.state.CategoryName;
const data = {
Category_ID,
CategoryName
}
fetch(REQUEST_URL, {
method: 'POST',
body: JSON.stringify(data),
headers: { 'Content-Type': 'application/json' }
})
.then(response => {window.location.href = "/";})
.catch(() => console.log("error in posting data"));

}
}

相关内容

最新更新