skip to Main Content

I’m trying to write an AJAX script that will log me out of Laravel using a POST request:

$.ajax({
        headers: {
        'X-CSRF-Token': "{{ csrf_token() }}"
    },
    type: "POST",
    url: '/logout',

    success: function() {
        window.location.replace('https://portal.nchinc.com/?logout=timeout');
    }
});

The result is I just get redirected, the logout doesn’t take effect. What am I doing wrong?

Note: The CSRF token is being set to avoid a 401 unauthorized HTTP response.

Thanks for any help!

2

Answers


  1. try like:

    $.ajax({
        type: "POST",
        url: '/logout',
        data: {'_token' : "{{ csrf_token() }}"},
        success: function() {
            window.location.replace('https://portal.nchinc.com/?logout=timeout');
        }
    });
    

    You should not need the header to be set, but _token is required to avoid Csrf validation failure.

    Login or Signup to reply.
  2. It works

    <meta name="csrf-token" content="{{ csrf_token() }}">
    
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search