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

查看:21
本文介绍了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, { // <== use axios.post
         data: this.data,
         _method: 'patch'                   // <== add this field
    })
    .then(function (response) {
         console.log(response);
    })
    .catch(function (error) {
         console.log(error);            
    });
}

这篇关于axios.patch/axios.put 在 Vue 和 Laravel 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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