Adding a Bearer Token to a GET request using location.href: A step-by-step guide

At the moment, I have all my ajax requests sending an Authentication token. However, I have created a service for exporting to excel and since ajax doesn't support that, I am using location.href for the get request. Is there a way to include the token in this process?

 var getUrl = '/LandingView/ExportToExcel?excelHeader=' + excelHeader;
                        getUrl += '&companyId=' + $scope.options.companyId;
                        getUrl += '&startDate=' + $scope.options.startDate.toJSON();
                        getUrl += '&endDate=' + $scope.options.endDate.toJSON();
                        getUrl += '&page=' + $scope.options.page;
                        getUrl += '&stateFilter=' + $scope.options.stateFilter;
                        getUrl += '&rows=' + $scope.options.rows;
                        getUrl += '&alertFilter=' + $scope.options.alertFilter;
                        getUrl += '&mode=' + $scope.options.mode;
                        getUrl += '&showLanguages=' + $scope.options.showLanguages;

 location.href = getUrl;

When I use this method, I need to make sure the bearer token is included in the header.

PS: Just wanted to mention that I am using ASP.NET MVC on the backend and AngularJS on the frontend.

Answer №1

I came across a very helpful article that provided me with valuable information. Here is the link to it.

The concept behind this method involves using ajax to retrieve a file saved in a temporary folder with a randomly generated name (guid). After obtaining the filename, the file can be downloaded using location.href, and then subsequently deleted.

https://www.codeproject.com/Tips/1156485/How-to-Create-and-Download-File-with-Ajax-in-ASP-N

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

In the provided Javascript snippet, how would you classify `word` - a function, variable, or object?

Understanding that an object is a variable and a function is a type of object, I find myself confused about the proper way to reference word in the following code snippet: var word; exports.setWord = function(c, ch){ word = c.get('chats')[ch]; ...

How can audio be efficiently streamed to the browser in small chunks using JavaScript?

I am currently working on setting up an internet radio station where I want to easily switch songs and overlay sounds. My goal is to limit the audio rate so that the feed can be adjusted before being sent out. Additionally, I would like to provide continuo ...

Tips for informing flowtype of expanding a partial options object to ensure it is fully developed by a specific stage

Most of you are probably familiar with a very simple use case from your projects. Imagine you have a utility class or function that looks like this: type Options = { foo?: string }; class Something { static get defaultOptions(): Options { ...

The issue arises where a string is detected instead of a boolean value in the schema when generating a list using the

Embarking on my journey as a Mailchimp application developer, I encountered an issue while attempting to create a list through the Mailchimp API. Has anyone else experienced this error? Below is the HTML code I used for the checkbox input: <input id ...

Using AngularJS in combination with a Flask backend to visualize the data being sent from the backend

I am currently developing a Single Page Application using Angular with a Python/Flask backend that connects to MongoDB. The challenge I'm facing is that, although I can retrieve data from the backend using a $http get request in Angular as an object ...

When exporting an enum in StencilJS with TypeScript, the error "Cannot find name..." may occur

Looking for a solution: https://github.com/napolev/stencil-cannot-find-name In this project, there are two main files to consider: custom-container.tsx import { Component, Element, State } from '@stencil/core'; @Component({ tag: 'cu ...

The TransferList component in Material UI does not update its state based on props when using the useState

My TransferList component in Material UI receives an array of previously selected items as props.selectedItems. The props.Items contains all available items. I expected to see props.selectedItems in the left panel of TransferList, but the left panel is em ...

Can Handlebar.js function without the use of Ajax?

I am looking to implement handlebar.js templates on my website in order to showcase a list of articles. However, I am concerned that by using handlebar.js, I will be restricted to calling my data through an API via ajax request. This could potentially ma ...

Encountering an issue while attempting to generate a dialog popup with jQuery

As a beginner in jQuery, I am attempting to implement a popup dialog box for users in case of an error. However, I'm encountering issues with the following jQuery code: <script type="text/javascript"> $(document).ready(function() { var $dia ...

Struggling to locate the index of the matching object within an array of objects?

There is a dataset available: var data = { "variants": [{ "quantity": "20", "varientId": 8, "currency": "YEN", "extraField": { "Size": "1 ...

The image fails to display when using THREE.js and Panolens.js

Trying to create a 360-degree environment with informational buttons using THREE.js and Panolens.JS However, I'm unable to resolve why the image is not appearing. Continuously encountering the error: Uncaught ReferenceError: process is not defined. ...

The printing function for the window system can cause disruptions to the layout of the table

After creating a page with a simple table that can be filled in and printed, I noticed some issues with the table formatting after printing. The intended table design was supposed to look like this: https://i.stack.imgur.com/aAk89.png However, upon print ...

Is it possible for a button to simultaneously redirect and execute a function using asynchronous JavaScript?

After browsing through several related posts, I realized that most of them were using href to redirect to a completely new webpage. However, in my JavaScript code, I have a button that utilizes the Material-UI <Link>. <Button component={Link} to= ...

Traverse JSON object as a string

I'm new to working with JavaScript. I have a JSON string that was created using the Google Gson API, and I'm passing this string to a JavaScript function. The JSON string looks like this: '{"validationCode":"V10291","caseNumber":"2010CF1010 ...

How can JavaScript/jQuery be used to update LocalStorage Objects when editing a form?

Having trouble pinpointing an issue with my code. Despite making modifications, the values in localStorage are not updating as expected. Any suggestions on what may be causing this problem? Note: Changing const idx to const i resulted in only the final va ...

Is there a way for me to retrieve the name of a newly opened browser tab from the original tab?

I have written a code snippet to create a new tab within the same browser by clicking on a link. function newTab() { var form1 = document.createElement("form"); form1.id = "newTab1" form1.method = "GET"; form1.action = "domainname"; //My ...

A simple method in JavaScript/TypeScript for converting abbreviations of strings into user-friendly versions for display

Let's say I am receiving data from my backend which can be one of the following: A, B, C, D Although there are actually 20 letters that could be received, and I always know it will be one of these specific letters. For example, I would like to map A ...

Guide to accessing a newly opened window from a different domain originating from the current window

Currently working on an Angular project, I am facing a scenario where I have a link on page A that directs users to a different origin page B. The HTML code for the link is shown below: ... <a href="https://another.origin"> PAGE B </a> ... On ...

JavaScript - Modifying several object properties within an array of objects

I am attempting to update the values of multiple objects within an array of objects. // Using a for..of loop with variable i to access the second array and retrieve values const AntraegeListe = new Array(); for (let i = 0; i < MESRForm.MitarbeiterL ...

Tips for adjusting the time interval on an automatic slideshow when manual controls are in play

I'm having trouble with my slideshow. I want it to run automatically and also have manual controls, but there are some issues. When I try to manually control the slides, it takes me to the wrong slide and messes up the timing for the next few slides. ...