如何使用axios处理400个错误

时间:2020-07-20 23:12:06

标签: php laravel axios

我有laravel项目,该项目使用AXIOS代码存储来自url的数据。但是,网址已损坏或返回错误400。进度已停止...

如何避免错误,并且该过程仍然继续。我已经使用了throw error代码,但仍然无法正常工作

这是我的代码

var url = 'https://www.imagephotolink.com';

axios.get(url, { responseType: "blob" }).then(function (response) {
    if (response.status === 200) {
       //catch and store process
    }else {
        throw new Error("Error");
    }
});

如何避免出现错误400,并且仍然可以继续执行该过程

1 个答案:

答案 0 :(得分:0)

您需要使用catch来承诺管理错误。 More info

您的代码应如下所示:

var url = 'https://www.imagephotolink.com';

axios.get(url, { responseType: "blob" }).then(function (response) {
  //catch and store process
}).catch(function (response) {
  //handle errors like 400, 404, etc
});