老师我设置了withCredentials:true刷新还是返回登录按钮

老师我设置了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;


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,测试你的代码是没有问题的。是谷歌浏览器的问题,建议使用火狐浏览器测试i一下。

祝学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师