Jquery Ajax request not saving cookies

Viewed 104

Iam sending ajax request with jquery from domain1 to domain2.

here is code:

let settings = {
        url: "https://domain2.com",
        method: "POST",
        headers: {
            "Access-Control-Allow-Credentials": true,
            "Access-Control-Allow-Origin": "https://domain1.com",
        },
        contentType: "application/json",
        data: JSON.stringify(data),
        xhrFields: {
            withCredentials: true
        }
    };
    jQuery.ajax(settings)
    .then( res => {
       // Do something with result
    );

but after request when iam checking browser cookies the response cookies not there. iam also checked in network tab that domain2 sending the cookies.

anyone knows where is problem ?

2 Answers

Use in ajax settings:

let settings = {
   url: "https://domain2.com",
   crossDomain: true,
   ... your settings
}

I found the problem, it was my bad.

All the settings above was true, the issue was about domain2 that set the cookies

with attribute SameSite = strict so domian1 could not set these cookies.

More informations: SameSite Cookies

Related