我试图在React应用程序中使用js设置一个有效期为10天的cookie。我遵循本文件,但是当我重新加载页面时,cookie的值始终是undefined。我希望它能使我设定的价值维持10天。
这是我设置cookie的代码:
handleClick() {
const axios = require('axios');
axios.post('http://127.0.0.1:8000/es/api/login/',
{
username: 'admin@admin.com',
password: 'Cancun10!',
//username: this.state.email,
//password: this.state.password.password,
},
)
.then(function (response) {
Cookies.set('x-xsrf-token', response.token, {expires: 10});
console.log(response);
})
.catch(function (error) {
console.log(error);
})
}这是我得到cookie值的代码:
class App extends Component {
render() {
var csrfCookie = Cookies.get('x-xsrf-token')
if(csrfCookie === 'undefined'){
return (
<div className="App">
<LoginModal />
</div>
);
} else {
return (
<div className="App">
<Albums />
</div>
)
}
}
}
export default App;我希望if第一次发送到LoginModal,但之后每次发送到Albums,持续10天。
发布于 2019-01-09 15:05:18
我发现了错误。cookie是可以的,但是我正在比较cookie与'undefined'的值,但我必须将它与undefined进行比较。一旦我改变了这样的条件:if(csrfCookie === undefined),一切都正常。
https://stackoverflow.com/questions/54103563
复制相似问题