【问题标题】:axios get, if error (or 404), call axios postaxios get,如果错误(或404),调用axios post
【发布时间】:2018-12-11 00:23:12
【问题描述】:

我有一个 axios 调用来从数据集中获取 UPC。如果成功了我就做点什么,如果没有找到,再次调用axios进行POST。

这就是我现在所拥有的,但是当它在尝试运行 get 时出错 (404) 时,我得到一个 422 错误。

axios.get('http://localhost:3000/api/products/findOne', {
  "params": {
    "filter": {
      "where": {
        "upc": product.upc
      }
    }
  }
}).then(response => {
  // UPC Found
}).catch(function(error) {
  // UPC Not found
  // Post UPC 
  axios.post('http://localhost:3000/api/products', {
  //axios stuff here
  }).then((response) => {
    console.log(response);
  }).catch((error) => {
    console.log(error);
  });
});

我根本不相信我这样做是正确的...输入?

【问题讨论】:

    标签: javascript ajax post get axios


    【解决方案1】:

    HTTP 错误 422 表示 无法处理的实体 如果找不到记录,您请求的 API 看起来像是 422 而不是 404?

    【讨论】:

    • 它在 catch 错误中给出 422。我可以在其中运行任何其他函数,而不是基本的 axios 调用。即使 ai 重复了一个 200 状态调用,仍然是 422。这没有意义。这也让我相信我做错了哈哈。
    • @RyanJay 这真是一个服务器错误!那里出了点问题。 JavaScript 代码没有任何问题。所以我会检查服务器日志为什么它发送 433
    猜你喜欢
    • 2021-11-05
    • 2018-12-02
    • 2018-05-03
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    • 2020-01-16
    • 2019-03-17
    相关资源
    最近更新 更多