#session after ajax request get expired

10 messages · Page 1 of 1 (latest)

cunning hollow
#

i am on some page with form, but after i make ajax post request to backend api, my session getting override so when i click next page will getting 419 | page expired error

inside the form, i added @csrf tag to generate _token hidden field. and also inside the ajax headers, added x-csrf-token

example code :

$.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                url: "{{route('routename')}}",
                type: 'POST',
                data: {
                   ...
                },
                beforeSend: function() {
                    $('.loading').removeClass('d-none');
                },
                success: function (data) {
                    $('.loading').addClass('d-none');
                    $('#carrier_id_'+seller_id).html(data.view)
                }
            });

anything i miss out? 🤔

cunning hollow
#

ps: ajax request is retrieve some option from server
and @hollow ferry is inside the form

fleet shadow
#

So, I already replied to you in #general, but let's continue here to keep the convo contained. Since you're making a request with XHR and not loading the form at all the @csrf tag won't actually have an effect.
You'd want to remove the meta tag, then load the value from the cookie and send that along

topaz solstice
#

the jquery they are using gets the csrf token from the META tag, this is the correct approach. try doing a console.log for the success callback, and also do a console log for the error callback (youll need to add this). that will give you more details on why its messing up

topaz solstice
#

im assuming a redirect is happening from the response

#

which causes the expiration error

cunning hollow
#

hi @topaz solstice , there is no redirect happen during success, backend side will render the view and send it back

topaz solstice
#

ok thats great but what did the console logging i recommended output in your browsers console?

topaz solstice