【问题标题】:Getting error handler data from post request in AXIOS从 AXIOS 中的 post 请求中获取错误处理程序数据
【发布时间】:2021-09-18 21:58:28
【问题描述】:

你能告诉我如何在 axios 中使用来自 post 类型请求的响应,它会给出错误 422(无法处理的实体)吗?我无法使用来自 API 的 JSON 响应,确切地说,这将是错误处理,以及我需要的一些信息:example - { error: "ERROR_FROM_X_TO_Y" }(我需要错误属性)。

const postReq = () => {
  return async () => {
    try {
      const url = '/exampleUrl/account-data';
      try {
        const response = await axios.post(url);
      } catch (err) {
        console.log(err);
      }
    } catch (error) {
      console.log("error");
    }
  };
};

我已经尝试 console.log 响应,但它不起作用。

【问题讨论】:

    标签: javascript node.js error-handling axios request


    【解决方案1】:

    实际上,您的响应常量中存在错误,您是否尝试过:

    await axios.post(url).catch((err) => { console.error(err) });
    

    编辑:我在 github 上发现了这个问题,我希望她能对解决您的问题感到满意:

    axios.post('/formulas/create', {
                name: "",
                parts: ""
    }).then(response => { 
          console.log(response)
        }).catch(error => {
          console.log(error.response)
        });
       
    

    https://github.com/axios/axios/issues/960

    【讨论】:

    • 是的,我做到了。但是,我无法通过控制台得到错误。
    猜你喜欢
    • 2017-07-29
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-11
    相关资源
    最近更新 更多