代码之家  ›  专栏  ›  技术社区  ›  Rafid

axios.patch/axios.put在Vue和Laravel中不起作用

  •  5
  • Rafid  · 技术社区  · 8 年前

    我的Vue文件中有此代码

    saveProfile() {
        axios.patch('/api/pegawai/' + this.id, {
             data: this.data
        })
        .then(function (response) {
             console.log(response);
        })
        .catch(function (error) {
             console.log(error);            
        });
    }
    

    在我的Laravel控制器里

    public function update(Request $request, $id){
         return $this->sendResponse('request retrieved');
    }
    
    public function sendResponse($message){
        $response = [
            'success' => true,
            'message' => $message,
        ];
        return response()->json($response, 200);
    }
    

    当我运行Vue文件将数据传递给控制器时 它应该 给我一个JSON对浏览器控制台的响应,值如下

    {
      'success' : true,
      'message' : 'request retrieved'
    }
    

    但是 目前它给 我是个错误 500 internal server error 通过浏览器控制台。如果我替换,我会得到同样的结果 axios.patch 具有 axios.put .

    --- 我试过的 ---

    我试过了 axios.post axios.get axios.patch补丁 两者都能正常工作。岗位和获取控制器:

    public function store(Request $request){
        return $this->sendResponse('request retrieved');
    }
    
    public function index(){  
        return $this->sendResponse('request retrieved');
    }
    
    1 回复  |  直到 8 年前
        1
  •  6
  •   Hassanal Shah    8 年前

    HTTP PUT

    saveProfile() {
        axios.post('/api/pegawai/' + this.id, {
             data: this.data,
             _method: 'patch'
        })
        .then(function (response) {
             console.log(response);
        })
        .catch(function (error) {
             console.log(error);            
        });
    }