老师,我这里按课程的改了,但是登录后刷新还是要重写登录
来源:7-1 根据登录权限决定页面展示(1)
墨莫
2021-04-21 22:34:28
import React, { Component } from 'react';
import { Modal, Button, Input, message } from 'antd';
import './style.css';
import axios from 'axios';
class Login extends Component {
constructor(props) {
super(props);
this.state = {
login: false,
modal: false,
user:'',
password:''
};
this.showModal = this.showModal.bind(this);
this.hideModal = this.hideModal.bind(this);
this.changeUser = this.changeUser.bind(this);
this.changePassword = this.changePassword.bind(this);
this.checkLogin = this.checkLogin.bind(this);
}
checkLogin() {
const { user, password } = this.state;
let url = `http://www.dell-lee.com/react/api/login.json?user=${user}&password=${password}`;
axios.get(url,{withCredentials:true}).then((res) => {
const { login } = res.data.data;
console.log(login);
if (login) {
message.success('登录成功');
this.setState({ login,modal:false });
} else {
message.error('登录失败,请核对用户名密码后再次登录');
}
});
}
changeUser(e) {
this.setState({
user: e.target.value,
});
}
changePassword(e) {
this.setState({
password: e.target.value,
});
}
showModal() {
this.setState({ modal: true });
}
hideModal() {
this.setState({ modal: false });
}
render() {
const login = this.state.login;
return (
<div className="login-box">
{login ? (
<Button type="primary">退出</Button>
) : (
<Button type="primary" onClick={this.showModal}>
登录
</Button>
)}
<Modal
title="开始登录吧"
visible={this.state.modal}
onOk={this.checkLogin}
onCancel={this.hideModal}
>
<Input
placeholder="请输入用户名"
style={{ marginBottom: 10 }}
onChange={this.changeUser}
/>
<Input
type="password"
placeholder="请输入密码"
style={{ marginBottom: 10 }}
onChange={this.changePassword}
/>
</Modal>
</div>
);
}
componentDidMount() {
axios.get('http://www.dell-lee.com/react/api/login.json',{
withCredentials:true
}).then((res) => {
const { login } = res.data.data;
this.setState({ login });
console.log(login);
});
}
}
export default Login;
相关截图:

我也重启浏览器了
1回答
同学你好,请求是否登录的接口写错了,应该是isLogin.json

修改后就正确了,可以再测试下。
祝学习愉快!
相似问题