mardi 17 mars 2020

Getting CSRF token mismatch Laravel

I'm trying to submit a form with javascript but I keep getting this error CSRF token mismatch. I tried to change var url = ""; to ?_token= and it was submitting the form to the database without data. I have seen a lot of question similar to this but I didn't get a solution for my case.How can I fix this?

<form id="form" enctype="multipart/form-data">
    <input type="hidden" value="" id="token"/>
<div class="form-group" >
<label for="title">Title</label>
<input type="text" name="title" >
</div>

<div class="form-group">
<label for="description">Description</label>
<input type="text" name="description">
</div>
</form>
<input type='button' value="Submit" id="btn"/>

Javascript

var token = $("#token").val();
$(document).ready(function(){

$("#btn").click(function(event){
event.preventDefault();
var url = "";
var form = $('#form')[0];
var formData = new FormData(form);
formData.append('_token', token);
$.ajax({
    url: url,
    data: formData,
    type: 'POST',
    cache: false,
    contentType: false,
    processData: false,
    success:function(data){
    if($.isEmptyObject(data.error)){
    $("#msg").html("successfull");
    $("#msg").fadeOut(3000);
     }
    }
});
});

});

Route

  Route::resource('review','ProductReviewController');


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/33pErPA
via IFTTT

Aucun commentaire:

Enregistrer un commentaire