【问题标题】:Axios Request AbortedAxios 请求中止
【发布时间】:2022-02-10 20:37:22
【问题描述】:

我有一个 axios 发布请求,当我们单击按钮时会触发该请求。但是一旦单击按钮,它就会显示此错误并重新加载页面

触发请求的函数

const handleForm = async (e) => {
//Check username is unique
e.preventDefault();
const oldUserName = await doesUserNameExist(userName);
if (oldUserName) {
  activeUser.displayName = userName;
  const imgUrl = await uploadPhoto(activeUser?.displayName, profilePicture);
  const userFormData = {
    userName,
    bio,
    gender,
    age,
    uid: activeUser.uid,
    emailAddress: activeUser.email,
    accountCreatedOn: serverTimestamp(),
    lastSeen: serverTimestamp(),
    following: [''],
    followers: [''],
    role: 'user',
    status: '',
    photoURL: imgUrl ? imgUrl : activeUser?.photoURL,
  };

  //  await addUser(userFormData);
  const nodeData = {
    id: activeUser.uid,
    emailAddress: activeUser.email,
    userName,
  };
  await axios.post('path_to_api/api/user', nodeData, {
    headers: nodeData,
  }); 
}

请帮忙。提前致谢!!

【问题讨论】:

  • 为什么要将数据放在标题中?
  • 因为有中间件来验证与 cookie 传递的相同数据
  • 你能提供一个minimal reproducible example吗?

标签: javascript reactjs axios next.js


【解决方案1】:

试试这个:

 await axios.post('path_to_api/api/user', nodeData); 

【讨论】:

  • 还是不行..
  • 嘿,您触发该功能的按钮在 type="submit" 或 type="button" 中?
  • 都试过了也不行
猜你喜欢
  • 1970-01-01
  • 2020-07-15
  • 1970-01-01
  • 2017-11-28
  • 1970-01-01
  • 2021-06-01
  • 2018-06-28
  • 2022-01-28
  • 2019-10-01
相关资源
最近更新 更多