
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to set cookie value with AJAX request in JavaScript?
We are required to set cookies with AJAX requests or in such a way that any AJAX request sends those cookies to the server.
One thing to note here is that every AJAX request made to any remote server automatically sends all our cookies to that very server without us having to do anything. Therefore, with this thing clear, we just have to set a specific key to our document object using JavaScript and whenever we make a network call, that cookie will automatically be sent to the server which we are making the call to.
The code for setting the cookie will be −
const token = 'some 323 very 535 random 5445 value'; document.cookie = `token=${token}`;
If we want a cookie that expires at a certain time in future, we can create that with 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 network request the ‘token’ cookie will automatically be sent to the server.
- Related Articles
- How to set a cookie and get a cookie with JavaScript?
- How to serialize a cookie name-value pair into a Set Cookie header string in JavaScript?
- How to set JavaScript Cookie with no expiration date?
- How to share Session ID Cookie with another request using Postman?
- How to pass a CSRF token with an Ajax request in Laravel?
- How to set Multiple Tests for a Request in Postman with JavaScript Method?
- How to pass JavaScript Variables with AJAX calls?
- Add Authentication details in the AJAX request in SAPUI5
- How to disable some jQuery global Ajax event handlers for a request?
- How to set a cookie to a specific domain in selenium webdriver with Python?
- Explain the different ready states of a request in AJAX
- How to check if a cookie is set in Laravel?
- How to register a handler to be called when the Ajax request begins using jQuery?
- How to read a cookie using JavaScript?
- How do I create and read a value from the cookie in JavaScript?
