老師 加上{ withCredentials: true } 後刷新還是返回登入按鈕,怎麼回事

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

weixin_慕雪0272820

2020-08-28 23:30:01

import React, { Component, Fragment } from "react";
import { Button, Modal, Input,message } from "antd";
import axios from "axios";
import'./style.css'

class Detail extends Component {
  constructor(props) {
      super(props);
      this.clickLogin = this.showModal.bind(this);
      this.showModal = this.showModal.bind(this);
      this.hideModal = this.hideModal.bind(this);
      this.changeName = this.changeName.bind(this)
      this.changePassword = this.changePassword.bind(this)
      this.checkLogin = this.checkLogin.bind(this)
      
    
      this.state = {
        login: false,
        modal: false,
        name: "",
        password: ""
      };
      
  }   
    
    

    showModal() {
        this.setState({modal:true})
    }
    
    hideModal() {
        this.setState({modal:false})
    }

    changeName(e) {
        this.setState({name:e.target.value})
    }

    changePassword(e) {
        this.setState({ password: e.target.value });
        
    }

    checkLogin() {
                   const { name, password } = this.state;
        const url = `http://www.dell-lee.com/react/api/login.json?user=${name}&password=${password}`
        axios.get(url, { withCredentials: true }).then((res) => {
          const { login } = res.data.data;
          if (login) {
            message.success("登入成功");
            this.setState({
              login: true,
              modal: false,
            });
          } else {
            message.error("登入失敗");
          }

          // this.setState({ login })
        });
    }
    
    componentDidMount() {
        const url = 'http://www.dell-lee.com/react/api/islogin.json'
        axios.get(url, { withCredentials: true }).then(res => {
            const login = res.data.data.login
            this.setState({ login })
        })
        
    }

    render() {
        const { login } = this.state
        return (
          <Fragment>
            {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.changeName} value={this.state.name}/>
              <Input placeholder="請輸入密碼" type="password" value={this.state.password} onChange={this.changePassword} value={this.state.password} />
            </Modal>
          </Fragment>
        );       
  }
}
export default Detail;
写回答

1回答

好帮手慕星星

2020-08-29

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

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

祝学习愉快!

1

0 学习 · 10739 问题

查看课程