Issue encountered while attempting to upload a document in Angular due to Content-Disposition

I am having trouble uploading a file using Angular. Every time I make the request, an error occurs stating: "Request has been blocked by CORS policy: Request header field content-disposition is not allowed by Access-Control-Allow-Headers in preflight response."

Below is the code I am using:

  postXml(serviceName: string, data:any = {}){
    const url = environment.apiUrl + "/" + serviceName;
    let token = this.storageService.getWithoutAsync(AuthConstants.AUTH);
    const header = new HttpHeaders({
      'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
      'Authorization': `${token}`,
      'Content-Disposition':"attachment; filename='file.xls'"
    });
    return this.http.post(url, data, { headers: header})
  }

Answer №1

The reason for that error is due to making a HttpRequest to a different domain than the one your page is on. The browser blocks it because typically only requests from the same origin are allowed for security purposes. To resolve this, you need to allow multiple domains to call the API. For assistance, check out this resource.

For a better understanding, you can refer to this link on CORS.

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

Advantages of utilizing Angular libraries compared to Angular modules within a monorepo, exploring the NX architecture

What advantages does using libraries instead of modules in Angular offer, as suggested by nx.dev for a monorepo architecture? I can see the benefits for an npm publishable feature like interfaces that another repo will use, but why should I turn a busines ...

How come TypeScript does not detect when a constant is used prior to being assigned?

There's an interesting scenario I came across where TypeScript (3.5.1) seems to approve of the code, but it throws an error as soon as it is executed. It appears that the root cause lies in the fact that value is being declared without being initiali ...

Exporting constants using abstract classes in TypeScript files

In my Typescript files, I've been exporting constant variables like this: export const VALIDATION = { AMOUNT_MAX_VALUE: 100_000_000, AMOUNT_MIN_VALUE: 0, DESCRIPTION_MAX_LENGTH: 50, }; My constant files only contain this one export without any ...

Approach to activate Required Field Validation while navigating through DatePicker form control

Within my Angular application, I have implemented Required Field Validation for a DatePicker component: <div class="form-group" [ngClass]="{ 'has-required':['injuryDate'].untouched && ['injuryDate'].invalid, ...

Display or conceal specific elements within the ngFor loop

Looking for a way to show/hide part of a component in Angular2? Here's an example: <li *ngFor=" #item of items " > <a href="#" (onclick)="hideme = !hideme">Click</a> <div [hidden]="hideme">Hide</div> </li> If ...

Customizing modal window HTML in ng-bootstrapNeed to override the default modal window

Currently, I am utilizing ng-bootstrap to create my modals. I have been pondering the most effective approach to customize the modal window's HTML. The default appearance of the modal html is somewhat like this: <div role="document" class="modal- ...

The jsPDF tool captures only the visible frame in a screenshot instead of printing the entire content on the screen

Recently, I integrated the jsPDF npm module into my Angular application to convert HTML to PDF. However, I encountered an issue where printing a website page to PDF only captures a screenshot of the visible area in Firefox and Chrome, as well as in Interne ...

Angular 2 - JSON parsing error: Unexpected token < detected at the beginning of the file while interacting with WebAPI [HttpPost]

I'm having trouble making a POST request to my ASP.NET WebAPI endpoint from angular 2 http service. The endpoint doesn't seem to be getting hit at all, despite trying various solutions found in posts. In my angular 2 component, the code for call ...

Exploring Angular Unit Testing: A Beginner's Guide to Running a Simple Test

I'm diving into the world of angular unit testing and looking to set up my first successful test. Here's what I've come up with: import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { AppComponent } fro ...

Tips for modifying TypeScript class types based on a parent class object property?

As part of a project, I have created two classes: ParentClass childrenClass which extends the ParentClass class Displayed below is the code for my ParentClass: interface ConfSetup<T extends boolean> { enabled: T; permissions: bigint[]; locati ...

When using Framer Motion for page transitions alongside React Router DOM v6, the layout components, particularly the Sidebar, experience rerenders when changing pages

After implementing page transitions in my React app using Framer Motion and React-Router-DOM, I noticed that all layout components such as the sidebar and navbar were unexpectedly rerendering upon page change. Here's a snippet of my router and layout ...

Methods for organizing consecutive elements within an array in Javascript/Typescript

Let's explore this collection of objects: [ { key1: "AAA", key2: "BBB" }, { key1: "BBB", key2: "CCC" }, { key1: "CCC", key2: "DD ...

How to Retrieve Superclass Fields in Angular 5 Component

I have a superclass that provides common functionality for components. export class AbstractComponent implements OnInit { public user: User; constructor(public http: HttpClient) { } ngOnInit(): void { this.http.get<User>(& ...

Executing an API call in Angular using a for-loop

I'm working on a project where I need to make multiple API calls based on the length of a mockInput.json file. Here's how I have implemented it: api.service.ts import { Injectable } from '@angular/core'; import { HttpClient, HttpHeade ...

Angular4 encountered an error: $(...).DataTable is not recognized as a function

I utilized the bootstrap4 datatable jQuery function within my Angular4 application to construct a sortable table. Below is the code snippet. .component.ts import { Component } from '@angular/core'; declare var $: any; @Component({ template ...

User Interface showcasing real-time progress data pulled from API

Currently, I am facing a situation where there are three docker containers involved in my project: - An angular frontend - A Django backend - A Python processing API The scenario is such that a user uploads a file to the backend volume through the fronten ...

Leverage native dependencies in AWS CodeBuild for an Angular project

I am facing an issue with a build project on CodeBuild. One of the dependencies required for the project is located on the build machine itself, specifically in my own directory. The specific section in package.json that mentions this dependency is: &quo ...

What would be the best dimension for an Angular 11 module?

Picture yourself developing a brand new modular app in Angular and facing the decision of when to create a new module. Is it considered best practice to create a module per page if the pages do not have common components? Is there an optimal size for Ang ...

Is it possible to access BreakpointObserver in Angular Material before the page has finished loading?

When utilizing the Angular BreakpointObserver in combination with Angular Material, I encounter an issue where the observer only triggers a change after it detects a difference. This becomes problematic on initial page load as there is no existing change t ...

How can errors be captured when working with chained rxjs observables in an combineLatest scenario?

After reading through this thread, I encountered the following scenario: Observable.combineLatest( this.translate.get("key1"), this.translate.get(""), this.translate.get("key3"), this.translate.get("key4") ) .subscr ...