0

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. There's an error. Error CSRF missmatch.

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);
                }
            });
  • 1
    Does this answer your question? [Laravel csrf token mismatch for ajax POST Request](https://stackoverflow.com/questions/32738763/laravel-csrf-token-mismatch-for-ajax-post-request) – Nazgot Apr 30 '21 at 08:03

1 Answers1

0

You will have to define the csrf token in the main array and not in the 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)

Aless55
  • 2,652
  • 2
  • 15
  • 26