亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Vue JS 組件中發出 AJAX 刪除請求?

如何在 Vue JS 組件中發出 AJAX 刪除請求?

PHP
qq_笑_17 2023-08-11 16:39:51
我有一個 Vue JS 組件,其中包含一個模式,詢問用戶是否要使用“是/否”選項刪除特定記錄。我希望在單擊“是”按鈕時觸發 AJAX 刪除請求,現在我嘗試將 ajax 代碼移動到我的 Vue 組件中并使用 vue-resource。目前,刪除后我在 chrome devtools 控制臺中收到以下錯誤消息:app.js:38907 刪除http://127.0.0.1:8000/clients/2/delete 419(狀態未知)127.0.0.1/:1 未捕獲(承諾)響應 {url: "/clients/2/delete", ok: false, status: 419, statusText: "unknown status", headers: Headers, …}我嘗試過以下代碼:應用程序.js    Vue.component('client', require('./components/ClientComponent.vue').default);        /**     * Next, we will create a fresh Vue application instance and attach it to     * the page. Then, you may begin adding components to this application     * or customize the JavaScript scaffolding to fit your unique needs.     */        import VueResource from 'vue-resource';        Vue.use(VueResource);        const app = new Vue({        el: '.table-container',    });客戶端組件.vue    <template>        <li :data-clientID="client.id"><a :href="this.homeRoute">{{ client.first_name + ' ' + client.last_name }}</a>            <span class="delete_x" data-toggle="modal" v-bind:data-target="delete_modal" :data-model="client.id">x</span>            <div class="modal fade" v-bind:id="delete_id" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">                <div class="modal-dialog" role="document">                    <div class="modal-content">                        <div class="modal-header">                                      <h4 class="modal-title" id="myModalLabel">Are you sure you want to delete client {{ client.first_name + ' ' + client.last_name }}?</h4>                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>                        </div>
查看完整描述

1 回答

?
慕的地6264312

TA貢獻1817條經驗 獲得超6個贊

419 表示 csrf 令牌丟失或不匹配。


可以修改每個請求默認添加x-csrf-token。將此代碼放在使用vueResource之后


Vue.use(VueResource)


Vue.http.interceptors.push(function(request) {

  request.headers.set('X-CSRF-TOKEN', $('meta[name="csrf-token"]').attr('content'));

});

https://github.com/pagekit/vue-resource/blob/develop/docs/http.md#request-processing


查看完整回答
反對 回復 2023-08-11
  • 1 回答
  • 0 關注
  • 132 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號