我有一个编辑合同名称的功能。我使用特定 ID 向后端 API 调用 axios 请求。对于每种情况,我都称其为 sweetalert 消息。
axios({
url: '/api/contract/' + id,
method: 'put',
data: {
name: name
}
}) .then((response) => {
this.$emit('fetchAll');
swal({
title: "Success!",
icon: "success"
});
}) .catch(error => {
this.errors = error.response.data.errors;
swal({
title: "Error",
text: error.response.data.message,
icon: "error"
});
});
回应:
403: You are not authorized to edit this contract.

Laravel 控制器中的错误处理:
if (Bouncer::cannot('contract-destroy'))
abort('403', "You are not authorized to delete this contract");
即使请求出现错误,也会弹出成功消息。