Project Zero

Laravel Ajax

#Add on Top of ajax file for CSRF Token
#_Token: is hidden field with CSRF value

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('#_Token').val()
    }
});

#Ajax Function with header
var firstName   = $.trim($('.ctf-firstName').val());
var $_token     = $.trim($('#_Token').val());
$.ajax({
            url: $('#contact-form-url').val(),
            type: "POST",
            async: true,
            headers: {'X-XSRF-TOKEN': $_token},
            data: "firstName=" + firstName,
            success: function (data) {
               /* functionality */
            },
            error: function () {
               /* error message */
            }
        });
Exit mobile version