当状态码为 500 时,React 代码不显示错误

问题描述 投票:0回答:1

React 代码状态读取失败,因此当状态代码为 500 时不显示任何错误

尝试{ const res = await axios({ 方法:“发布”, 网址:

${landslide}
, 数据:表单数据, 标头:{'Content-type':“multipart/formData”} }); console.log("你好", res); 如果(res.data.status === 200){ swal("成功", res.data.message, "成功").then(函数() { 导航(“/滑坡”); }); } else if (res.data.status === 500) { swal("警告", res.data.message, "警告").then(函数() { 导航(“/滑坡”); }); } } 赶上(错误){ swal("warning", "请检查你的表单输入", "warning").then(function() { 导航(“/山体滑坡报告”); }); }

      }
reactjs laravel api
1个回答
0
投票

设置

try
catch
方法以更好地捕获错误。

try { 
  await getData();
} catch (error) {
 if(error.response.status === 500) { // your error
   // your code for that particular error
 }
}
© www.soinside.com 2019 - 2024. All rights reserved.