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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51170564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:52:14  来源:igfitidea点击:

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

laravelhttpvue.jsaxios

提问by Rafid

I have this code in my vue file

我的 vue 文件中有这段代码

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

在我的 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);
}

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

当我运行 vue 文件将数据传递给控制器​​时,它应该给我一个带有以下值的浏览器控制台的 json 响应

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

but currently it givesme an error 500 internal server errorthrough the browser console. I get the same result if I replace axios.patchwith axios.put.

目前它500 internal server error通过浏览器控制台给了我一个错误。如果我替换axios.patchaxios.put.

--- What I Have Tried---

---我尝试过的东西---

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

我试图做的axios.post,并axios.get用相同的方案作为axios.patch两者正常工作。发布和获取控制器:

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

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

回答by Hassanal Shah

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

实际上HTTP PUT不被html标准识别。尝试像这样进行黑客攻击:

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