Javascript 如果登录失败,如何验证

Javascript 如果登录失败,如何验证,javascript,reactjs,api,axios,Javascript,Reactjs,Api,Axios,如果条件是正确的,则重定向到主页,但如果条件是错误的,请不要执行任何操作,我希望如果条件是错误的,它将显示从响应API填充的警报,例如“STATUS_CODE=='400'”错误的电子邮件或密码 export class Login extends Component { constructor(props){ super(props) this.state = { MEMBER_EMAIL:'', MEMBER_PASSWORD:'',

如果条件是正确的,则重定向到主页,但如果条件是错误的,请不要执行任何操作,我希望如果条件是错误的,它将显示从响应API填充的警报,例如“STATUS_CODE=='400'”错误的电子邮件或密码

export class Login extends Component {
  constructor(props){
    super(props)
    this.state = {
      MEMBER_EMAIL:'',
      MEMBER_PASSWORD:'',
      device_type:'3',
      redirect: false,
      loading:false,
      error:''
    };
    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange(e){
    this.setState({
      [e.target.name] : e.target.value
    })
  }

  async handleSubmit(e){
    e.preventDefault()
    if(this.state.MEMBER_EMAIL && this.state.MEMBER_PASSWORD){
      await API.post('member/login', this.state)
      .then((response) => {
        let responseJson = response
        // console.log(responseJson.data)
        if(responseJson.data.STATUS_CODE === '200'){
          this.props.resSave('data', JSON.stringify(responseJson.data.DATA))
          this.setState({
            redirect: true
          })
        }
      })
    }
  }

  render() {

    if(this.state.redirect){
      return <Redirect to='/home' />
    }

    return (
      <Container>
        <Padding />
          <Row>
            <Col md={{ span: 6, offset: 3 }} className='mx-auto'> 
            <Card>
              <CardContent>
                <h3><b>Login</b></h3>
                <p><b>Welcome back to <i className='text-warning'>privilegez</i></b></p>
                <br />
                <Form onSubmit={this.handleSubmit}>
                  <Form.Group controlId='formBasicEmail'>
                    <Form.Label><b>Email or Username</b></Form.Label>
                    <Form.Control type='email' name='MEMBER_EMAIL' placeholder='Enter email' onChange={this.handleChange} />
                  </Form.Group>
                  <Form.Group controlId='formBasicPassword'>
                    <Form.Label><b>Password</b></Form.Label>
                    <Form.Control type='password' name='MEMBER_PASSWORD' placeholder='Password' onChange={this.handleChange} />
                  </Form.Group>
                  {/* <Partnership /> */}
                  <p className='text-muted float-right'><b>Forgot password?</b></p>
                    <Button type='submit' variant='warning' className='text-white' size='md' block>Login</Button>
                </Form>
                <br/>
                <p className='text-muted text-center'><b>Or</b></p>
                <p className='text-primary text-center'><b><i className='fa fa-facebook-official' aria-hidden='true'></i>&nbsp;Login with facebook</b></p>
                <br />
                <p className='text-muted text-center'><b>Dont have an account&nbsp;<Link to='/register'><span className='text-warning'>Register</span></Link></b></p>
              </CardContent>
            </Card>
          </Col>
        </Row>
      </Container>
    )
  }
}
导出类登录扩展组件{
建造师(道具){
超级(道具)
此.state={
成员电子邮件:'',
成员\u密码:“”,
设备类型:'3',
重定向:false,
加载:false,
错误:“”
};
this.handleChange=this.handleChange.bind(this);
this.handleSubmit=this.handleSubmit.bind(this);
}
手变(e){
这是我的国家({
[e.target.name]:e.target.value
})
}
异步handleSubmit(e){
e、 预防默认值()
if(this.state.MEMBER\u电子邮件和this.state.MEMBER\u密码){
等待API.post('member/login',this.state)
。然后((响应)=>{
让responseJson=response
//console.log(responseJson.data)
如果(responseJson.data.STATUS_CODE==='200'){
this.props.resSave('data',JSON.stringify(responseJson.data.data))
这是我的国家({
重定向:true
})
}
})
}
}
render(){
if(this.state.redirect){
返回
}
返回(
登录
欢迎回到privilegez


电子邮件或用户名 密码 {/* */}

忘记密码了吗

登录

使用facebook登录


没有帐户注册

) } }

我已经试过好几次了,如果条件不正确,那么他什么也没做,如何在条件不正确时显示警报提交表单时自动刷新你的应用程序

为什么不将handleSubmit函数移到Button的onClick事件中呢

这是我的推荐信

async handleSubmit(e){
    e.preventDefault()
    if(this.state.MEMBER_EMAIL && this.state.MEMBER_PASSWORD){
      await API.post('member/login', this.state)
      .then((response) => {
        let responseJson = response
        // console.log(responseJson.data)
        if(responseJson.data.STATUS_CODE === '200'){
          this.props.resSave('data', JSON.stringify(responseJson.data.DATA))
          this.setState({
            redirect: true
          })
        } else if (// your condition // ) {
          // you do something here
      })
    }
  }
将onSubmit从中移除,然后像这样更改按钮

<Button onClick={this.handleSubmit} variant='warning' className='text-white' 
size='md' block>Login</Button>
登录