我有一個 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">×</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
- 1 回答
- 0 關注
- 132 瀏覽
添加回答
舉報
0/150
提交
取消