简体   繁体   English

如何在 laravel 中使用 object 数据数组发布 ajax

[英]How to ajax post with array of object data in laravel

I want to ask you here.我想在这里问你。 I have an array of objects like this on this array of objects.我在这个对象数组上有一个像这样的对象数组。 I want to use it as a data post to the controller when I run it.我想在运行它时将它用作 controller 的数据发布。 There's an error.有一个错误。 Error CSRF missmatch.错误 CSRF 不匹配。

my code我的代码

var dataarray=[];
             dataarray.push({
                _token:$('#_token').val(),
                no_draft :$('#no_draft').val(),
                no_transaction :$('#no_transaction').val(),
                currency :$('#currency').val(),
                kurs_total_idr :$('#kurs_total_idr').val(),
                no_referensi :$('#no_referensi').val(),
                grant_total_ap :$('#grand_total_ap').val(),
                payment_type :$('#payment_type').val(),
                grant_total_payment :$('#grand_total_payment').val(),
                payment_account :$('#payment_account').val(),
                payment_account_id :$('#payment_account_id').val(),
                note :$('#note').val(),
                date :$('#datepicker1').val(),
                grant_total_payment :$('#grand_total_payment').val(),
                status :$('#status').val(),
                acc_tra_ap_payment_detail:acc_tra_ap_payment_detail,
            });
            var url = "";
            savemethod="add"
            if (savemethod === 'add') {
                url = "<?php echo url('ap-payment/store') ?>";
            } else {
                url = "<?php echo url('/updatepegawai') ?>";
            }
            $.ajax({
                url: url,
                contentType: 'application/json',
                type: "POST",
                data: dataarray,
                dataType: "JSON",
                success: function (data) {
                    console.log("berhasil",data);
                },
                error: function (request, status, error) {
                    alert("Error json " + error);
                }
            });

You will have to define the csrf token in the main array and not in the object.您必须在主数组中定义 csrf 令牌,而不是在 object 中。

so your data array has to look like:所以你的数据数组必须看起来像:

dataarray = 
[
_token:$('#_token').val(),
data: {...}
]

and then it should recognize the csrf token (make also sure that the vlaue is present)然后它应该识别 csrf 令牌(还要确保 vlaue 存在)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM