I have a bunch of POST requests made with Ajax in my Laravel application.
A typical query looks like this:
$.ajax({
url: '/path/to/method',
data: {'id': id},
type: 'POST',
datatype: 'JSON',
success: function (response) {
},
error: function (response) {
}
});
I have a set of CSRF tokens and everything works fine:
jQuery(document).ready(function(){
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
});
However, after a long break (for example, the computer falls asleep for a long time), all Ajax calls return error 419, as if the token had not been set. After reloading the page, everything returns to normal. This is located on the local server.
How do i solve this? Is there a way to “refresh” a token before a call? Do I need to make a bit $.ajaxSetupbefore every call? Is this not enough to do it once at page load?
jovan source
share