Axios删除不起作用

时间:2018-07-17 08:00:20

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

在基于Laravel 5.6和Vue.js 2的CRUD Web应用程序中,添加,编辑和显示正常运行。但是删除不起作用,因为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();

}

Home.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');

newfile.txt

App \ Sms对象 (     [hidden:protected] =>数组         (             [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] => *
    )

1 个答案:

答案 0 :(得分:2)

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

delete