can you put three backticks ``` before and after your code
May 13, 2017
6
Level 1
How do i use csrf_token in an external javascript file
I am using datatables to update a record on the db, I use an external javascript file to append the form to the modal section on my blade file. I want to be able to update the record from the external javascript file but I keep getting internal server error. I am using the csrf_token from the meta tag which I have tried placing in the external js file and also in the blade file.
External JS File
'<div class="form-group">' +
'<label class="col-sm-2 control-label">Distributor Name</label>' +
'<div class="col-sm-10">' +
'<input type="hidden" id="id" name="id" value="' + $('<div/>').text(data[0]).html() + '">' +
'<input class="form-control" type="text" id="name" name="name" value="' + $('<div/>').text(data[1]).html() + '">' +
'</div>' +
'</div>' +
'<div class="form-group">' +
'<label class="col-sm-2 control-label">Distributor Address</label>' +
'<div class="col-sm-10">' +
'<textarea class="form-control" type="text" name="address" id="address">' + $('<div/>').text(data[2]).html() + '</textarea>' +
'</div>' +
'</div>' +
'<div class="form-group">' +
'<button type="submit" class="btn btn-primary">Update</button>' +
'</div>' +
'</form>' +
'<meta name="_token" content="{!! csrf_token() !!}" />';
$('.row-name', $dlg).html(data[1]);
$('.modal-body', $dlg).html(html);
var frm = $('#form');
//alert(index);
frm.on('submit', function(ev) {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
}
});
ev.preventDefault();
//alert(index);
var index = $('#id').val();
var name = $('#name').val();
var address = $('#address').val();
console.log(getBaseURL);
//alert(name);
$.ajax({
type: "POST",
url: getBaseURL + '/distributor/edit/'+index,
data: {name: name, address: address},
success: function( msg ) {
//$("#ajaxResponse").append("<div>"+msg+"</div>");
//location.reload();
sessionData = msg.sessionData;
$('#form').trigger("reset");
}
});
});```
Please or to participate in this conversation.