axios.patch/axios.put在Vue和Laravel中不起作用 [英] axios.patch / axios.put is not working in Vue and Laravel
问题描述
我的Vue文件中有此代码
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);
});
}
以及在我的laravel控制器中
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);
}
当我运行vue文件以将数据传递到控制器时,它应该为我提供具有以下值的浏览器控制台的json响应
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'
}
但当前它通过浏览器控制台给我一个错误500 internal server error
.如果将axios.patch
替换为axios.put
,则会得到相同的结果.
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
.
--- 我尝试过的东西 ---
我尝试在与axios.patch
相同的情况下执行axios.post
和axios.get
,并且两者均正常工作.发布和获取的控制器:
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');
}
推荐答案
实际上HTTP PUT
不能被html标准识别.尝试像这样进行黑客入侵:
actually HTTP PUT
is not recognized by html standard. try to do hack like so:
saveProfile() {
axios.post('/api/pegawai/' + this.id, {
data: this.data,
_method: 'patch'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
这篇关于axios.patch/axios.put在Vue和Laravel中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!