axios 可能的未处理的Promise拒绝/错误:请求失败,状态代码为400

ss2ws0br  于 2023-10-18  发布在  iOS
关注(0)|答案(5)|浏览(209)

我知道有一些关于这个问题的答案,我都读过了。但都无济于事。这是我的错误消息:

这是我的行动:

export function registerUser(data){
const request = axios({
    method: "POST",
    url: `${REGISTER}${API_KEY}`,
    data: {
        email: data.email,
        password: data.password,
    },
    headers:{
        "Content-Type":"application/json"
    }
}).then(response => response.data)

return {
    type: "REGISTER_USER",
    payload: request,
}

}
谢谢你,谢谢

mbjcgjjk

mbjcgjjk1#

给予一个尝试获取库进行API调用。

function registerUser(data){
    return fetch(url, {
        method: 'POST',
        body: JSON.stringify(data),
        headers:{
            'Content-Type': 'application/json'
        }
        })
        .then(res => res.json())
        .then((apiResponse)=>{ 
            console.log("api response", apiResponse) 
            return {
                type: "REGISTER_USER",
                api_response: apiResponse.data
            }
        })
        .catch(function (error) {
            return {
                type: "REGISTER_USER",
                api_response: {success: false}
            }
        })
}

实现上述功能

let data = { 
 email: "[email protected],
 password:"yourpassword"
}   

registerUser(data).then((response)=>{
  console.log(response)
})
v440hwme

v440hwme2#

记录错误和成功,然后检查:

export function registerUser(data){
const request = axios({
    method: "POST",
    url: `${REGISTER}${API_KEY}`,
    data: {
        email: data.email,
        password: data.password,
    },
    headers:{
        "Content-Type":"application/json"
    }
 })  
 .then(function (response) {
    // handle success
    console.log(response);
  })
 .catch(function (error) {
    // handle error
    console.log(error);
  })
yx2lnoni

yx2lnoni3#

无论在哪里使用promise调用API,都应该使用catch处理程序,因为当API失败时,您必须处理错误。

export function registerUser(data){
  return axios({
    method: 'post',
    url: `${REGISTER}${API_KEY}`,
    data: {
        email: data.email,
        password: data.password,
    },
    headers: {
       'Content-Type': 'application/json'
    }})
    .then(function (response) {
        //handle success
        return {
            type: "REGISTER_USER",
            payload: response.data,
        }
    })
    .catch(function (err) {
        //handle error
        return {
            type: "REGISTER_USER_FAILED",
            payload: null
        }
    });
}

像这样调用函数

const data = {
    email: '[email protected]',
    password: 123
}
registerUser(data).then((response)=>{
  console.log(response)
})
omtl5h9j

omtl5h9j4#

export function registerUser(data){
  return axios({
      method: "POST",
      url: `${REGISTER}${API_KEY}`,
      data: {
        email: data.email,
        password: data.password,
      },
      headers:{
        "Content-Type":"application/json"
      } 
  }).then((api_response)=>{ 
    return {
      type: "REGISTER_USER",
      api_response: api_response.data
    }
  }).catch(function (error) {
    return {
      type: "REGISTER_USER",
      api_response: {success: false}
    }
  })
}

//Invoking the above function
 let data = { 
     email: "[email protected],
     password:" password"
   }      
registerUser(data).then((response)=>{
  console.log(response)
})
1aaf6o9v

1aaf6o9v5#

在我的情况下,问题是畸形的身体发送到后端。

相关问题