我想在javascript laravel中添加csrf_field()

时间:2017-06-01 04:51:47

标签: javascript php jquery laravel laravel-5.4

(function () {
        new _vue2.default({
            el: 'body',

            data: {
                tableData: {
                    options: {
                        sortable: true,
                        editable: true,
                        pageCount: 10
                    },

                    columns: [{
                        value: 'id',
                        text: 'No',
                        sortable: true,
                        editable: false
                    }, {
                        value: 'name',
                        text: 'Name',
                        sortable: false,
                        editable: false,
                        isHTML: true
                    }, {
                        value: 'items',
                        text: '#of Items',
                        sortable: true,
                        editable: false
                    }, {
                        value: 'total',
                        text: 'Total',
                        sortable: true,
                        editable: false
                    },  {
                        value: 'click',
                        text: '',
                        sortable: false,
                        editable: false,
                        isHTML: true
                    },],

                    rows: [],

                    onPageChanged: function onPageChanged(page) {
                        console.log('Current page is ' + page);
                    }
                }
            },

            ready: function ready() {
                console.log(invoice.length);
                var invoices = invoice;
                var length = invoice.length;

                for (var i = 0; i < length; i++) {
                    var obj = {
                        id: {
                            value: i + 1
                        },

                        name: {
                            value: '<a href="update/'+ invoices[i].id + '">' + invoices[i].invoice + '</a>'

                        },

                        items: {
                            value: invoices[i].items ,

                        },

                        total: {
                            value: invoices[i].alltotal ,

                        },



                        click: {
                            value: '<a href="pdf/'+ invoices[i].id + '">PDF</a>&nbsp;&nbsp;  <form method="POST" action="remove/'+ invoices[i].id + '"><input type="hidden" name="_method" value="DELETE"><button type="submit">Remove</button></form>'
                        },
                    };

                    this.tableData.rows.push(obj);
                }
            },


            components: {
                DataTable: _DataTable2.default
            }
        });
    })();

我想在click:{}中创建删除表单,因为我想在laravel 5.4中使用rout delete功能。 我的html表是用vue.js构建的。我需要在vue.js中编写html代码。我收到此错误(VerifyCsrfToken.php第68行中的TokenMismatchException :)。如果有人知道请回答我。

2 个答案:

答案 0 :(得分:1)

首先,在您的信息页中添加{{ csrf_token() }}

然后在删除表单中添加以下行。

<input type="hidden" name="_token" value="' + document.getElementsByName('_token')[0].value + '">

答案 1 :(得分:0)

<head>标记中添加此内容:

<meta name="csrf-token" content="{{ csrf_token() }}" />