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');
}
6
  • So what is the actual response content coming back with the 500? It should contain a clue to the problem. Commented Jul 4, 2018 at 9:43
  • Could you please show the route declaration for these APIs? @Rafid Commented Jul 4, 2018 at 9:46
  • what error are you getting ? it will be easy if you add error in your question Commented Jul 4, 2018 at 9:47
  • @rkj the console only gives me 500 internal server error without other information Commented Jul 4, 2018 at 9:49
  • 1
    check laravel.log file, it will have details Commented Jul 4, 2018 at 9:55

1 Answer 1

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);            
    });
}
Sign up to request clarification or add additional context in comments.

2 Comments

what if i'm using formData?
@FernandoTorres formDataInstance.append('_method', 'patch')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.