We need to set the cookie via AJAX request, or set the cookie with any AJAX request Send these cookies to the server.
One thing to note here is that every AJAX request is automatically sent to any remote server Sends all of our cookies to this server without us having to do anything. So, with this Things are clear, we just need to set a specific key for our document object using JavaScript, Whenever we make a web call, this cookie is automatically sent to the server where we Calling.
The code to set a cookie is-
const token = 'some 323 very 535 random 5445 value'; document.cookie = `token=${token}`;
If we want a cookie that expires at some time in the future, we can create it using the following code-
const token = 'some 323 very 535 random 5445 value'; const expiry = 'Wed, 4 Feb 2021 12:00:00 UTC'; document.cookie = `token=${token} expires=${expiry}`;
This way, When we make any web request, a "token" cookie will automatically be sent to server.
The above is the detailed content of How to set cookie value using AJAX request in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!