繁体   English   中英

从 React 向 Django 发送 post 请求时出现 400(错误请求)

[英]400 (Bad Request) when sending a post request from React to Django

我正在尝试使用 react 和 redux 创建一个简单的登录页面。 当我单击登录按钮时,前端和后端都收到错误400(Bad Request) 我正在使用 simplejwt 身份验证。

I have the following as part of the redux action (auth.jsx)

export const login = (email, password) => async dispatch => {
    const config = {
        headers: {
            'Content-Type':'application/json'
        }
    };
    const body = JSON.stringify({email, password});
    try{
        const res = await axios.post(`${process.env.REACT_APP_API_URL}/user/login`, body, config)

        dispatch({
            type: LOGIN_SUCCESS,
            payload: res.data
        })
        dispatch(load_user());
    }   catch (err){
        dispatch({
            type: LOGIN_FAIL,
    
        })
    }
        console.log('You hit the wrong url')
}

Redux DevTools 在 try 和 catch 块中显示 catch ``type(pin):"LOGIN_FAIL"`


Login.jsx

const Login = ({ login }) => {
  const [formData, setFormData] = useState({
      email: '',
      password: ''

  });

  const {email, password} = formData;
  

  const onChange = e => setFormData ({ ...formData, [e.target.name]: e.target.value});
  const onSubmit = e => {
        e.preventDefault();
        login(email, password);
  }
  return(
      <div className='container'>
          <Form onSubmit={e => onSubmit(e)}>
                <Form.Group className="mb-3" controlId="formBasicEmail">
                    <Form.Control type="email" placeholder="example@email.com" onChange={e => onChange(e)} required />
                </Form.Group>
                <Form.Group className="mb-3" controlId="formBasicPassword">
                    <Form.Control type="password" placeholder="Password" minLength='6' onChange={e => onChange(e)} required />
                </Form.Group>
                <Button variant="primary" type="submit">
                    Login
                </Button>
                
        </Form>
      </div>
  )
//  const mapStateToProps = state => ({

//  })
}

export default connect(null, {login}) (Login)

登录后端 API 可以从await axios.post( ${process.env.REACT_APP_API_URL}/user/login part of the code where REACT_APP_API_URL=http://localhost:8000 . The problem now is that when I send a post request by clicking on the submit button to login a user, the content of the form电子邮件和密码. The problem now is that when I send a post request by clicking on the submit button to login a user, the content of the form does is not passed along because the error on both frontend and backend is POST http://localhost:8000/user/login 400 (Bad Request )` 我该如何解决这个问题,或者更确切地说,我出了什么问题?

登录 URL 错误 使用基于令牌的身份验证应该是http://localhost:8000/rest-auth/login/ 你需要休息框架。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM