代码之家  ›  专栏  ›  技术社区  ›  Daddi Al Amoudi

删除时,Axios将位置而不是ID发送到控制器

  •  0
  • Daddi Al Amoudi  · 技术社区  · 6 年前

    我是vue js的新手,我发现有些地方有点混乱,但这里是我面临的最新困惑。

    DELETE http://simvuecore/api/contoh/0 404 (Not Found)
    

    这意味着axios发送 position 当控制器期望 id 尽管

    作为参考,以下是我的代码:

    API routes :

    Route::delete('/contoh/{contoh}', 'contohController@destroy');
    

    contohController

    public function destroy(contoh $contoh)
    {
        $contoh->delete();
        return response('terhapus', 200);
    }
    

    store.js :

    state: {
        contoh: []
    
    },
    mutations: {
            deleteContoh(state, id) {
                const index = state.contoh.findIndex(item => item.id == id)
                state.contoh.splice(index, 1)
            }
    },
    actions: {
        deleteContoh(context, id) {
            axios.delete('api/contoh/' + id)
                .then(response => {
                    context.commit('deleteContoh', id)
                })
        },
    }
    

    contohItem.vue :

    methods: {
            removeContoh(id) {
                this.$store.dispatch('deleteContoh', id)
    
            },
    

    POSTMAN 当vue要求时, delete 是唯一一个因为这个不起作用的 位置 身份证件

    问题: 如何告诉axios发送 身份证件 而不是

    更新: 我把这个放进去了 继续项目.vue :

        <span @click="removeContoh(index)" class="remove-contoh">X</span>
    

    在我把它改成这样之后它就起作用了:

        <span @click="removeContoh(id)" class="remove-contoh">X</span>
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Andrey Popov    6 年前

    从你的密码我可以看出你通过了 id deleteContoh removeContoh 问题是-你没有通过 身份证件 index

    祝你好运!