当后端返回BadRequest或者NoFound时候,前端会认为这是个异常
来源:5-3 【应用】返回正确的 Status Codes

一日看尽长安花花
2021-03-24
当后端返回BadRequest或者NoFound时候,前端会认为这是个异常,从而无法正确解析响应头,这种情况可以通过后端处理吗?
写回答
1回答
-
hymanzhan
2021-04-23
后端可以通过中间件的方式改写相应中返回的错误信息内容,但前端要解析响应头,应该还是在前端调用api作对应的错误处理时解析吧…
fetch("https://localhost:5001/api/Buggy/notfound") .then((resp) => { if (!resp.ok) { for (const value of resp.headers.values()) { console.log(value); // 获得响应头内信息 } throw resp; } return resp.json(); }) .catch((err) => { err.json().then((errorMsg) => { console.log(errorMsg); }); });
10
相似问题