我的vue文件中有此代码

saveProfile() {
    axios.patch('/api/pegawai/' + this.id, {
         data: this.data
    })
    .then(function (response) {
         console.log(response);
    })
    .catch(function (error) {
         console.log(error);            
    });
}

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

当我运行vue文件将数据传递给控制器it should时,请使用以下值向浏览器控制台发送json响应

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

currently it gives通过浏览器控制台向我显示了一个错误500 internal server error.如果我把axios.patch换成axios.put,我会得到同样的结果.

--- What I Have Tried ---

我试着用与axios.patch相同的场景来做axios.postaxios.get,并且两者都能正常工作.投递和获取控制器:

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

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

推荐答案

实际上,HTTP PUT并不是html标准所认可的.try 这样做:

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

Vue.js相关问答推荐

无法从CDN使用Vue和PrimeVue呈现DataTable

检测Vue3/Vue-test-utils中的Vuex还原/Mutations

在VITE-VUE APP-DEV模式上重定向HTTP请求

vuejs中如何获取数组总长度

在 Vue 中删除自定义组件代码重复的方法?

Vuejs - 更新数组中对象的数组

VueJS:在组件之间使用全局对象的最佳实践?

VuetifyJS:如何摆脱 v-stepper 组件的提升?

将props传递给设置时的VueJS 3组合API和TypeScript类型问题:类型上不存在属性 user用户

aspnet core如何在deploy上构建webpack

如何从 Vuex 状态使用 Vue Router?

正确使用 Vue $refs

我可以使用react路由创建别名路由吗?

了解 Nuxt.js 中的State状态和Getters:Getters不起作用

如何在 Vuetify DataTable 组件中设置初始每页行数值?

如何使用 Vuetify 验证复选框组

使用 VueJS 使用 3rd 方库打印元素

升级到 vue-cli 3 后,HTML 中的空格消失了

在Vue中调用方法时的括号

在~/components/AddPlaceModal.vue中找不到导出AddPlaceModal