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:
$.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(){}....
...,
});
$.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 ).