What is the best way to set a default header for DELETE requests in AngularJS?

Everything seems to be functioning properly in AngularJS.

$http.defaults.headers.post['X-CSRFToken'] = $cookies['csrftoken'];
$http.defaults.headers.put['X-CSRFToken'] = $cookies['csrftoken'];
$http.defaults.headers.patch['X-CSRFToken'] = $cookies['csrftoken'];

However, there is an issue with the following:

$http.defaults.headers.delete['X-CSRFToken'] = $cookies['csrftoken'];

Answer №1

According to the Angular documentation on $http, Angular comes with default header configurations for common, put, and post methods only.

If you need to add headers for an HTTP method other than POST or PUT, simply create a new object with the lowercase HTTP method name as the key.

$httpProvider.defaults.headers.get = { 'My-Header' : 'value' }.

In your specific scenario,

$httpProvider.defaults.headers.delete = { 'X-CSRFToken' : $cookies['csrftoken'] }.

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

Calculation Error in JavaScript/JQuery

I've been working on a JavaScript function to calculate the sum of values entered into textboxes, but it seems to be giving me inaccurate results in certain cases. Check out the FIDDLE here Enter values : 234.32 and 32.34 Result: 266.6599999999999 ...

Adding a Tooltip to a Boxplot Graph in C# is Easier Than You Think

I have successfully generated a dynamic box plot chart and now I am looking to enhance it by adding a tooltip feature. The goal is to display the values of the box plot series in a tooltip when a user hovers over it. Below is the code snippet that I used ...

When render returns another component, React does not invoke componentWillMount

Here is my code setup: const Dashboard = React.createClass({ getInitialState(){ return { user: JSON.parse(localStorage.getItem('user')) }; }, componentWillMount(){ var self = this; $.get({ url: 'http://127 ...

Avoiding Duplicate Form Submissions Without JavaScript

Currently, I am working on a project where I am implementing the MVC pattern. Upon successful submission of a form, I redirect the user and display a flash message indicating success using session data. Users face no issues when using the back button or re ...

Continuous scrolling generates new pagination links as I continue to scroll

Thanks to the helpful comment from lharby on my previous post, I finally figured out how to implement Infinite Scrolling. Big thank you! The only issue now is that the script starts generating a new pagination link after scrolling past 15 posts (which i ...

Presenting Videogular: Displaying Either Images or Videos

In my AngularJS app, I utilize Videogular for playing videos. I have two lists - one for videos and another for images. When a user selects a video from the list, Videogular starts playing the selected video. However, if the user selects an image from the ...

extract the information from the JSON structure

Currently, I am in the process of learning JSON. $.ajax({ async: true, type: "POST", url: "fetch.....data.jsp", data: "vendorId="+vendor, success: function(json){ alert( "Received Data: " + ...

Changing the Position of HTML Scripts in React

I am facing an issue where I need to relocate an external script within a specific section of my page. However, I am unable to access the CSS attributes associated with this item. It seems that it is displayed as an iFrame, making it difficult to modify th ...

React: Improve performance by optimizing the use of useContext to prevent unnecessary re-renders of the entire app or efficiently share data between components without causing all

In my app, I have a Header.tsx component that needs to be accessible on all pages and a Home.tsx component where most of the content resides. The Home.tsx component includes an intersectionObserver that utilizes the useContext hook (called homeLinks) to p ...

Comparing Google's Material Design to Material Design Lite

Currently, I am working on a Django project and considering using Google's Material Design. However, I am facing challenges in setting up the environment with npm. I am curious about the advantages and disadvantages of each option. From what I have g ...

Developing a Modal Dialog Using AngularJS Controller

Creating a Modal Dialog directly within an HTML file is achievable, but when it comes to triggering the popup from a controller after performing some logic, such as service calls and data validation, things get tricky. The goal is to inform the user about ...

Solving issues with Angular4 Router changes

I'm attempting to chain the router resolver for my application. Below are my Router options: { path: '', component: AdminComponent, resolve: [ SessionResolve, LocaleResolve ] } The desired flow is to first call S ...

Fix image that won't load in Firefox

My website features an <img /> element that initially lacks a src attribute. The src is added dynamically using JavaScript at a later point. While Chrome hides it, Firefox displays an empty space: https://i.sstatic.net/3eZJ4.png Is there a way to p ...

Guidance on implementing a source map in a Node.js VM

Currently, I am analyzing JavaScript bundled source code in Node.js using the following snippet of code: const javascriptCode = "..." const javascriptSourceMap = "..." const wrapper = NativeModule.wrap(javascriptCode); const script = ne ...

What is the most Pythonic way to check if a function has been called with a specific argument in Python?

Looking at my Django codebase, I have an endpoint that performs various tasks. My goal now is to verify if this endpoint is calling the function do_metadata_request with a specific token. Here's the function for reference: def do_metadata_request(url_ ...

Can we save javascript-generated HTML audio as a file on the back-end server?

In my latest project, I am building a JavaScript sequencer that controls HTML audio using intervals and timeouts. The goal is to handle all the processing and recording on the back-end while displaying a "Processing..." message to the user, and then utili ...

JavaScript allows for the immediate termination of CSS animations

I am currently working on an input box that has a CSS animation applied to it. This animation creates a smooth fade-in effect when the page is loaded. Here is the CSS code: #search-box { /* ... */ animation: 2s fade-in } @keyframes fade-in { ...

Troubles with loading images on Node.js, Express, and EJS powered Bootstrap 5 navbar

Currently, I am in the process of creating a website using express/node.js/ejs. However, I am facing challenges when it comes to constructing a navbar with Bootstrap 5.0. In my app.js file, I have included express.static: app.use(express.static('publi ...

Is it necessary for an application utilizing Django models to also be a Django app?

Can you clarify the definition of a Django application for me? Is it any application that utilizes Django features, such as orm and url-view mapping? I am curious because I have a component with two sub-components: a web service server and a standalone ap ...

Leveraging babel-cli on your local machine

Is it possible to utilize the babel client without the need for global installation? Instead of following this method npm install -g babel-cli I am looking to achieve the same outcome by using npm install babel-cli --save-dev ...