登录之后 刷新页面之后还是登录

来源:7-1 根据登录权限决定页面展示(1)

陈立天

2020-11-01 20:26:55

# 具体遇到的问题
使用ie浏览器测试我的代码没有问题,为什么用chrome浏览器就这样?是我写的代码不兼容chrome浏览器了么所以保存不到cookie?
# 报错信息的截图

# 相关课程内容截图

# 尝试过的解决思路和结果

# 粘贴全部相关代码,切记添加代码注释(请勿截图)

在这里输入代码,可通过选择【代码语言】突出显示

import React, { Component } from 'react';

import { ModalButtonInput ,messagefrom 'antd';

import axios from 'axios'

import './style.css'



class Login extends Component {


    constructor(props) {

        super(props);

        this.state = {

            login: false,

            modal: false,

            user: '',

            password: ''

        }


        this.showModal = this.showModal.bind(this);

        this.onCancel = this.onCancel.bind(this);

        this.onOk = this.onOk.bind(this);

        this.changeUser = this.changeUser.bind(this);

        this.changePassword = this.changePassword.bind(this);

    }


    showModal() {

        this.setState({

            modal: true

        })

    }


    onCancel() {

        this.setState({

            modal: false

        })

    }


    onOk() {

        const {user,password} = this.state

        const 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.login

                if(login){

                    message.success('登录成功');

                    this.setState({

                        login:true,

                        modal:false

                    })

                }else{

                    message.error('登录失败');

                }

            })

        console.log(user,password);

    }


    changeUser(e){

        this.setState({

            user:e.target.value

        })

    }

    changePassword(e){

        this.setState({

            password:e.target.value

        })

    }


    render() {

        const { login } = this.state

        return (

            <div className='login'>

                {

                    login ?

                        <Button type="primary">退出</Button> :

                        <Button type="primary" onClick={this.showModal}>登录</Button>

                }

                <Modal

                    title="登录"

                    visible={this.state.modal}

                    onOk={this.onOk}

                    onCancel={this.onCancel}

                    okText='确定'

                    cancelText='取消'

                >

                    <Input

                        placeholder="请输入用户名"

                        style={marginBottom: 10 }}

                        value={this.state.user}

                        onChange={this.changeUser}

                    />

                    <Input

                        placeholder="请输入密码"

                        type='password'

                        value={this.state.password}

                        onChange={this.changePassword}

                    />

                </Modal>

            </div>


        )

    }


    componentDidMount() {

        axios.get('http://www.dell-lee.com/react/api/islogin.json',{

            withCredentials:true

        })

            .then(res => {

                const login = res.data.data.login;

                this.setState({ login })

                console.log(login)

            })

    }


}

export default Login;


写回答

1回答

好帮手慕星星

2020-11-02

同学你好,测试代码是没有问题的,登录之后再刷新是‘退出’效果

http://img.mukewang.com/climg/5f9f6d5c0908bf9702220104.jpg

Chrome浏览器可能是版本的问题。由于Chrome80版本请求接口未携带cookie导致的,因为浏览器升级后对跨域接口默认的安全策略变了,需要手动修改策略,打开谷歌浏览器,地址栏输入chrome://flags/ ,然后再弹出的页面中输入SameSite,把第一个改为disable,重启浏览器,再测试就可以了

http://img.mukewang.com/climg/5f9f6d7f0999bd3816220856.jpg

祝学习愉快!
0

0 学习 · 10739 问题

查看课程