Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
Send a cookie in Ajax Post request headers

I'm writing a script for user authentication & I would like to include a cookie in the ajax post request headers, I have tried two options:

  1. using the beforeSend method:
    $.ajax({
        type: 'post',
        url: 'http://example.com/login',
        data: request,
        cache: false,
        contentType: false,
        processData: false,
        crossDomain: true,
        beforeSend: function(xhr) {
          xhr.setRequestHeader(Cookies.set('CookieName', value, { path: '/', SameSite: 'Lax' });
        },
        success: function(){}....
      ...,
    });
  1. using the setRequestHeader in the success function:
  $.ajax({
     type: 'post',
     url: 'http://example.com/login',
     data: request,
     cache: false,
     contentType: false,
     processData: false,
     crossDomain: true,
     success: function(response, status, xhr) {
        let token = xhr.getResponseHeader('MyValue');

        xhr.setRequestHeader(Cookies.set('CookieName', token, { path: '/', SameSite: 'Lax' });

  })

What's the best way of doing it? I'd like to redirect the user to a different page on successful login, ( based on the cookie value ).

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!