Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

http - axios.patch / axios.put is not working in Vue and Laravel

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');
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...