【问题标题】:Axios request in react反应中的axios请求
【发布时间】:2020-09-30 21:45:10
【问题描述】:

我正在尝试从注册表单向使用 json 服务器制作的假 api 发出发布请求,但我不断收到错误 400 Bad Request。

const managerRegister = () => {

    const [manager, setManager] = useState({
    email: "",
    password: "",
    confirmPassword: "",
    accessLevel: 2,
    company: "",
  })
  
  console.log(manager)

  const handleSubmit = (values) => {
      axios.post(`https://reembolsa-ai-api.herokuapp.com/register`, manager)
      .then(res => setManager(res))
      .catch(error => console.log(error))
  }
    
I'm using an ant-design form so it already comes with a function onFinish that handles the actions to be taken when the form is submitted 

const onFinish = (values) => {
    handleSubmit(values)
  };

我尝试在 useEffect 中使用 axios 请求,但它也不起作用。

})

我需要将这些信息传递给 api。我试过了

【问题讨论】:

  • status 400 表示您正在向服务器发送不正确的内容。请查看您的网络标签以获取更多信息。

标签: reactjs axios react-hooks


【解决方案1】:

这段代码对我来说很好用。我认为您应该像这样填写参数。

import React, {useEffect} from 'react';
import axios from 'axios';
export default function ManagerRegister() {
  const [manager, setManager] = useState({
    email: "tester1@gmail.com",
    password: "tester",
    confirmPassword: "tester",
    accessLevel: 2,
    company: "Test",
  }) 
  
  const handleSubmit = (values) => {
    axios.post(`https://reembolsa-ai-api.herokuapp.com/register`, manager)
    .then(res => setManager(res))
    .catch(error => console.log(error))
 }
}

【讨论】:

  • 因为您的 api 要求电子邮件属性存在且唯一。如果您想获得成功响应,您应该更改电子邮件值
猜你喜欢
  • 2021-07-15
  • 2021-10-21
  • 1970-01-01
  • 2021-02-04
  • 2020-12-24
  • 2018-01-14
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
相关资源
最近更新 更多