2

我有一个编辑合同名称的功能。我使用特定 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");

即使请求出现错误,也会弹出成功消息。

4

2 回答 2

0

对于您的甜心,将密钥更改icontype. 否则我认为默认情况下会显示成功消息。所以代码应该读

axios({
       url: '/api/contract/' + id,
       method: 'put',
       data: {
             name: name
       }
       }) .then((response) => {
          this.$emit('fetchAll');
          swal({
               title: "Success!",
               type: "success"
          });
       }) .catch(error => {
          this.errors = error.response.data.errors;
          swal({
             title: "Error",
             text: error.response.data.message,
             type: "error"
       });
});
于 2018-09-27T14:15:52.843 回答
0

我现在发现了问题,回调参数应该定义为Object。我不知道为什么会出现问题,但现在已修复。

axios({
   url: '/api/contract/' + id,
   method: 'put',
   data: {
         name: name
   }
   }) .then(({data}) => { <-------instead of (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"
   });

});

于 2018-09-27T14:32:46.967 回答