目前,我的前端React应用程序中有一些 JavaScript 代码,这些代码使get/post requests我的Express后端使用axios. 如果输入无效,我当前的后端实现有时可能redirect会转到不同的页面(到以下示例中的登录页面)
//back-end logic
if(valid data) response.json(someData);
if(invalid data) response.redirect("/login");
Run Code Online (Sandbox Code Playgroud)
我的 axios 请求如下:
axios.get("some.url/api").then(res => {
//work with the res.someData if valid
}
.catch(err => console.log(err))
Run Code Online (Sandbox Code Playgroud)
我面临的问题是,如果axios收到重定向响应,它会获取该页面,但我想将窗口重定向到重定向的 URL。
是否可以使 axios 在收到来自 的消息时将用户窗口重定向到 axios 重定向的response.redirect('url')页面Express?
我用它来发送注册电子邮件:
sendMail($this->input->post('register_email'), $this->config->item('activation_sender_mail'), $this->config->item('server_name').'Account Creation', 'Hey '.$this->input->post('register_username').'r\n Thank you for your interest in contributing to the site.\r\n Just click this link to activate your account:\r\n <a href="'.$link.'">'.$link.'</a>');
Run Code Online (Sandbox Code Playgroud)
但我收到的邮件是:
嗨用户!感谢您有兴趣为该网站做出贡献.只需点击此链接即可激活您的帐户:链接
我想看起来像:
Hey user!
Thank you for you interest in contributing to the site.
Just click this link to activate your account:
link
Run Code Online (Sandbox Code Playgroud) 我有以下代码
axios
.get("some.url")
.then(res => {
console.log(...res.data);
this.setState({
isLoaded: true,
assignmentData: {...res.data}
});
console.log(this.assignmentData) //this returns undefined
})
Run Code Online (Sandbox Code Playgroud)
它从后端获取一些数据并将其保存到状态变量中。从服务器发送的数据格式如下
[{id:1, title:"abc"},{id:2,title:"bcd"}]。但是,assignmentData由于以下console.log返回未定义,因此不会使用 setState 更新的值。
如何assignmentData使用从服务器接收到的数据更新状态?