13

I have this code in my vue file

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

and in my laravel controller

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);
}

when i run the vue file to pass the data to the controller it should give me a json response to the browser console with the following value

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

but currently it gives me an error 500 internal server error through the browser console. I get the same result if I replace axios.patch with axios.put.

--- What I Have Tried ---

I have tried to do axios.post and axios.get with the same scenarios as axios.patch and both working properly. Controller for post and get:

public function store(Request $request){
    return $this->sendResponse('request retrieved');
}

public function index(){  
    return $this->sendResponse('request retrieved');
}
Rafid
  • 641
  • 1
  • 8
  • 20
  • So what is the actual response content coming back with the 500? It should contain a clue to the problem. – Joe Jul 04 '18 at 09:43
  • Could you please show the route declaration for these APIs? @Rafid – Adre Astrian Jul 04 '18 at 09:46
  • what error are you getting ? it will be easy if you add error in your question – rkj Jul 04 '18 at 09:47
  • @rkj the console only gives me `500 internal server error` without other information – Rafid Jul 04 '18 at 09:49
  • @AdreAstrian this is the route declaration `Route::resource('pegawai', 'Pegawai\PegawaiAPIController');`. the file name of the controller is `PegawaiAPIController.php` – Rafid Jul 04 '18 at 09:52
  • 1
    check `laravel.log` file, it will have details – rkj Jul 04 '18 at 09:55

1 Answers1

22

actually HTTP PUT is not recognized by html standard. try to do hack like so:

saveProfile() {
    axios.post('/api/pegawai/' + this.id, { // <== use axios.post
         data: this.data,
         _method: 'patch'                   // <== add this field
    })
    .then(function (response) {
         console.log(response);
    })
    .catch(function (error) {
         console.log(error);            
    });
}
Hassanal Shah
  • 354
  • 2
  • 5