老师我设置了withCredentials:true刷新还是返回登录按钮
import React,{Component} from "react"
import { Modal,Button,Input,message} from 'antd';
import axios from "axios"
import "./style.css"
class Login extends Component{
constructor(props){
super(props);
this.showModal=this.showModal.bind(this);
this.handleHide=this.handleHide.bind(this);
this.changeUser=this.changeUser.bind(this);
this.changePassword=this.changePassword.bind(this);
this.checkLogin=this.checkLogin.bind(this);
this.logout=this.logout.bind(this);
this.state={
login:false,
modal:false,
user:"",
password:"",
}
}
showModal(){
this.setState({modal:true});
}
handleHide(){
this.setState({modal:false});
}
changeUser(e){
// console.log(e.target);s
this.setState({user:e.target.value});
}
changePassword(e){
this.setState({password:e.target.value});
}
logout(){
axios.get("http://www.dell-lee.com/react/api/logout.json",{withCredentials:true})
.then(res=>{
const logout=res.data.data.logout;
if(logout){
this.setState({login:false})
}
// console.log(logout);
})
}
checkLogin(){
const {user,password}=this.state;
// console.log(user,password);
let url=`http://www.dell-lee.com/react/api/login.json?user=${user}&password=${password}`;
axios.get(url,{withCredentials:true})
.then(res=>{
// console.log(res);
const login=res.data.data.login;
// console.log(login);
if(login){
message.success("登录成功");
this.setState({modal:false,login:true});
}else{
message.error("登录失败");
}
});
}
render(){
const {login}=this.state;
return (<div className="login">
{
login ?
<Button
onClick={this.logout}
type="primary">退出</Button> :
<Button type="primary"
onClick={this.showModal}
>登录</Button>
}
<Modal
title="登录"
visible={this.state.modal}
onOk={this.checkLogin}
onCancel={this.handleHide}
>
<Input
placeholder="请输入用户名"
onChange={this.changeUser}
value={this.state.user}
style={{marginBottom:10}} />
<Input
placeholder="请输入密码"
onChange={this.changePassword}
value={this.state.password}
type="password" />
</Modal>
</div>);
}
componentWillMount(){
axios.get("http://www.dell-lee.com/react/api/login.json",{withCredentials:true})
.then(res=>{
const login=res.data.data.login;
this.setState({login})
})
}
}
export default Login;
正在回答
同学你好,测试你的代码是没有问题的。是谷歌浏览器的问题,建议使用火狐浏览器测试i一下。
祝学习愉快~
- 参与学习 人
- 提交作业 239 份
- 解答问题 10739 个
本阶段带你深入前端开发的肌理,通过ES6基础知识和前端主流高级框架的学习,助你快速构建企业级移动webAPP应用,进入职场的终极battle
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星