axios.patch/axios.put在Vue和Laravel中不起作用 [英] axios.patch / axios.put is not working in Vue and Laravel

查看:454
本文介绍了axios.patch/axios.put在Vue和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.postaxios.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆