【发布时间】:2018-09-27 16:23:38
【问题描述】:
我希望在提交 POST 请求后在按钮单击时刷新数据表。
我有一个反应按钮:
<Button onClick={this.click} className="domain-button" type='primary'>Add</Button>
以及对应的click函数,以及refreshPage函数:
async click() {
const { domainInputValue } = this.state;
console.log( domainInputValue );
const response = await
fetch('/new-cert', {
headers: {
'Content-Type': 'application/json'
},
method: 'POST',
body: JSON.stringify({domainInput: domainInputValue})
});
const body = await response.text()
console.log(body)
if (response.status !== 200) throw Error(body.message);
return body;
}
refreshPage() {
window.location.reload();
}
在我的后端 Nodejs 中处理我的 POST 请求:
app.post('/new-cert', function (req, res) {
fs.appendFile('certs-list', '\n' + req.body.domainInput, function (err) {
if (err) throw err;
});
res.send('POST request: ');
console.log("INSERTING")
exec('sh cert-check-script-insert.sh');
console.log(req.body);
console.log('post is working!')
});
我不确定在哪里调用refreshPage(),我尝试在click() 之后立即调用它,但这似乎为时过早,并且刷新不会更改表格中显示的数据。
【问题讨论】:
-
您可以在
state中有一个posted变量,该变量在POST 之后会发生变化。这将导致组件重新渲染,这似乎是您想要的。 -
@Colin 谢谢你的建议,你能不能写一个答案,只是不知道把东西放在哪里
标签: javascript node.js reactjs refresh page-refresh