What is the process for injecting a cookie value into the Authorization header prior to making an XmlHttpRequest

Observed result image; for a clearer understanding, please refer to the following link:
https://i.sstatic.net/WouIU.png

Here is the code snippet: When a user clicks on the button, all HTTP headers are retrieved except for the Authorization Header. This header is crucial for obtaining the response, so it was hardcoded as

xhttp.setRequestHeader ('Authorization', 'Bearer')
. However, I require its value since each user will have a different authorization value. You can view all the headers in the snapshot below.

The Authorization header has been hardcoded in the code.

<html>
<body>
<button type='button' onclick='cors()'>CORS</button>
<p id='demo'></p>
<script>
function cors() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var a = this.responseText;
document.getElementById("demo").innerHTML = a;
xhttp.open("POST", "http://xyz . com", true);
xhttp.withCredentials = true;
console.log(a);
xhttp.send("data="+a);
}
};
xhttp.open("GET", "https://api. xyz. com/v1/users/", true);
xhttp.withCredentials = true;
xhttp.setRequestHeader("Authorization", "Bearer ");
xhttp.send();

}
</script>
</body>
</html>

I aim to include only the cookie '[test_token]' value into the Authorization header. When the test_token is added, the header should read as 'Authorization: Bearer test_token'. The cookie must be appended to the authorization header before displaying the response.

Answer №1

It is impossible.

If you want to place the cookie inside the Authorization header, you will have to retrieve it using JavaScript.

Because the cookie is located on the outgoing request, and since this is a cross-origin request, the cookie is positioned on a different origin, meaning it cannot be accessed.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Managing memory and CPU resources in NodeJS while utilizing MongoJS Stream

Currently, I am in the process of parsing a rather large dataset retrieved from MongoDB, consisting of approximately 40,000 documents, each containing a substantial amount of data. The dataset is accessed through the following code snippet: var cursor ...

Utilize Parse cloud code to navigate and interact with object relationships

I am currently in the process of developing a Parse cloud code function that will produce a similar outcome to a GET request on parse/classes/MyClass, but with the IDs of the relations included. While I have successfully implemented this for a single obje ...

Use PHP and jQuery to convert HTML to JSON from a specified URL and generate a tree view of the HTML elements

When a URL is entered into a textbox and the "OK" button is clicked, the left side of the page displays a preview of the HTML while the right side shows a tree view of the HTML tags (body, header, div, span, etc.) as shown in the attached image. The expect ...

Restart MagnificPopup

I've been working on implementing Magnific Popup after a successful AJAX call, but I'm encountering some difficulties. It seems like I may need to reinitialize the magnificPopup, which I have attempted without success so far. Here's my curre ...

Sending data from a Node.js backend to a React.js frontend using res.send

How can I pass a string from my nodejs backend using res.send? app.post("/user", (req,res) => { console.log(req.body.email); res.send('haha'); }); I need to perform certain operations on the front end based on the value of the string retriev ...

Angular: Observing changes in the store and sending a message from a Service component to another component once the Service has finished specific tasks

Within our codebase, we introduce two classes known as GetDataAsyncService. This service is designed to wait for a change in the store before executing the block of code contained within it. By utilizing observables and subscribing to data changes with t ...

Sharing real-time data components in Vue 2.0 with others

Update: I've recently implemented VUEX to efficiently manage and store all my data. I'm currently facing an issue with data synchronization between components in a container. Although each component's data is stored within the container, it ...

Executing javascript after an AJAX call within an on() method: Tips and tricks

I am struggling to make a $.ajax() call within a function triggered by the .on() method, in order to execute a script on a .click() event after updating newly appended data. I have tried numerous times to troubleshoot, but I can't seem to pinpoint the ...

The 'ui.bootstrap' module is currently unavailable in AngularJS

Currently, I am encountering a peculiar issue with my angularjs project. On my website - www.server.com/pwm (home page), there is an anchor tag that redirects me to another page - www.server.com/publishers. When I navigate from the home page to the publis ...

Generating navigation links with search queries

Embarking on the journey of incorporating pagination alongside user-defined search parameters has left me puzzled. I have a good grasp on pagination implementation without user input, but once user input factors in, confusion sets in. I find myself at a lo ...

Steps for "submitting" a portion of a form with jQuery

I have a form that resembles a spreadsheet layout. My goal is to submit specific fields from a row to the server using jQuery Ajax whenever the user navigates away from that row. The challenge arises because the page consists of one large form, making it n ...

Determining the width of an element in terms of percentage

My issue involves input elements with widths set as percentages in CSS under the common class 'display-class' For example: input.test1 { width: 60% } In JavaScript, I am attempting to wrap a div around these input fields with the same width ...

Is it possible to immobilize dynamic table columns seamlessly without resorting to an unsightly loop or being confined to

I have a table and I'd like to freeze the header row along with a specific number of columns. While I managed to achieve this, it's quite slow on larger tables. Is there a more efficient approach to get the same outcome? I experimented with using ...

Tap to navigate to queued sections on click

Greetings, community members! Despite my extensive research, I have been unable to find a solution to my query. Just a heads up - I am a novice when it comes to javascript and jQuery. My question pertains to incorporating two image buttons that, upon cli ...

What is the method for checking the pathname condition?

Hello, I am attempting to create an if statement for the pathname, but I am having trouble getting it to work properly. if (pathname == "/") { category = 'home'; pagetype = 'homepage'; } If the pathname matches "/", the script ...

Ways to prevent a div from loading an HTML page

When an external button is clicked, the remove() function on id main is triggered. The issue arises when a user quickly clicks on btn1 and then presses the external button, causing the removal to occur before the event handler for btn1. This results in the ...

Tips on retrieving 'captureDate' from data points and dispatching it as a notification

Currently, I am working on adding a new feature to my discord bot that will allow it to collect the user's most recent gameclip. While I am able to gather all the necessary information in my console log, I am finding it challenging to figure out how t ...

Insert a fresh item into the existing unordered list

Every time I enter something in the prompt, it shows up as "undefined". What I actually want is for whatever I type into the prompt to be added as a new list item. For instance, if I type "Food" in the prompt, I expect to see "Food" appear on the second li ...

Tips for resolving the ExtPay TypeError when using Typscript and Webpack Bundle

I am currently trying to install ExtPay, a payment library for Chrome Extension, from the following link: https://github.com/Glench/ExtPay. I followed the instructions up until step 3 which involved adding ExtPay to background.js. However, I encountered an ...

When converting a Hebrew Excel sheet to JSON, question marks are generated in the output

Currently, I am facing an issue while trying to convert Excel (*.xlsx) to a JSON object in Node JS. The problem is that all the columns containing Hebrew characters end up getting converted into question marks. To illustrate the issue: https://i.sstatic. ...