【问题标题】:Axios Delete not workingAxios删除不起作用
【发布时间】:2018-12-24 20:19:43
【问题描述】:

在我基于 Laravel 5.6 和 Vue.js 2 的 CRUD webapp 中,添加、编辑和显示工作正常。但是删除不起作用,因为 axios.delete 没有将 id 发送到控制器。 Home.vue 中的 Console.log 显示正确的 id 和键值。以下是我在控制器中得到的代码和结果。请告诉我的错误。另外我真的需要在 web.php 中删除路由吗?

控制器

public function destroy(Sms $sms)
    {
        $myfile = fopen("newfile.txt", "w", true) or die("Unable to open file!");
        $txt = "Print_r: ". print_r($sms) ."\r\nID: ". $sms->id;
        //fwrite($myfile, $txt);
        fwrite($myfile, print_r($sms, TRUE));
        //die();
        Sms::where('id',$sms->id)->delete();

}

首页.vue

del(key,id){
            if(confirm("Are you sure?")){
                this.loading = !this.loading
                /*axios.delete(`/sms/${id}`)*/
                axios.delete(`sms/${id}`, {params: {id: `${id}`}})
                .then((response)=> {this.lists.splice(key,1);this.loading = !this.loading})
                .catch((error)=> this.errors = error.response.data.errors)
                console.log(`KEY:${key} ID:${id}`);
            }

}

web.php

Route::delete('sms/{id}', 'smsController@destroy');

新文件.txt

应用\短信对象 ( [隐藏:受保护] => 数组 ( [0] => created_at [1] => updated_at )

[connection:protected] => 
[table:protected] => 
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
    (
    )

[withCount:protected] => Array
    (
    )

[perPage:protected] => 15
[exists] => 
[wasRecentlyCreated] => 
[attributes:protected] => Array
    (
    )

[original:protected] => Array
    (
    )

[changes:protected] => Array
    (
    )

[casts:protected] => Array
    (
    )

[dates:protected] => Array
    (
    )

[dateFormat:protected] => 
[appends:protected] => Array
    (
    )

[dispatchesEvents:protected] => Array
    (
    )

[observables:protected] => Array
    (
    )

[relations:protected] => Array
    (
    )

[touches:protected] => Array
    (
    )

[timestamps] => 1
[visible:protected] => Array
    (
    )

[fillable:protected] => Array
    (
    )

[guarded:protected] => Array
    (
        [0] => *
    )

)

【问题讨论】:

  • 而且类名不区分大小写,对吧?
  • 不,这不是区分大小写的情况。

标签: laravel laravel-5 vue.js vuejs2 axios


【解决方案1】:

尝试将axios.delete更改为axios.post,并在要发送的数据中添加一个_method字段,其值为delete。像这样

axios.post(`sms/${id}`, {params: {id: `${id}`}, _method: 'delete'})

【讨论】:

  • 对不起,亲爱的,不工作。我试过以下: axios.delete(/sms/${id}) axios.delete(/sms/${id}, {params: {'id': id}}) axios.post(sms/${id}, {params: {id: ${id} }, _method: '删除'})
猜你喜欢
  • 2020-08-06
  • 2018-10-17
  • 2023-04-03
  • 2020-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多