Angular encountering a 405 Method not allowed error along with the "Provisional Headers are shown" message

It's really frustrating me. I'm attempting to make a simple request to my api server by adding the header, but it keeps showing me the message "Provisional Headers are shown" and then fails on the subsequent request.

  Provisional headers are shown
Accept: application/json, text/plain, */*
Access-Control-Allow-Credentials: true
Cache-Control: no-cache
Expires: Sat, 01 Jan 2000 00:00:00 GMT
Pragma: no-cache
Referer: https://mywebsite.com/
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36

When I include the following data in my intercept method, I encounter an error.

 intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    request = request.clone({ headers: request.headers.set('Access-Control-Allow-Credentials', 'true') }); 
    request = request.clone({ headers: request.headers.set('Cache-Control', 'no-cache') });
    request = request.clone({ headers: request.headers.set('Pragma', 'no-cache') });
    request = request.clone({ headers: request.headers.set('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT') });      
    
    return next.handle(request);
  }

If I remove it and don't add the headers, I avoid the error, but eventually, I will need to send other data in the header such as an authtoken and user id. Why am I encountering this 405 error when appending headers?

Also, why does it say "method not allowed" even though I clearly permit Options and Post methods? https://i.stack.imgur.com/i4Opp.png

Answer №1

A similar issue arose for me roughly a month back.

The solution involves generating a simple http ok response for the Options verb. To implement this, place the following code snippet in your base api controller. The Options request must receive an OK response callback initially to proceed with forwarding Options to the API.

 public HttpResponseMessage Options()
{
  return new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
}

Answer №2

Make sure to include an API Key in the header section with a designated key (e.g.: x-my-api-key) and corresponding value (e.g.: 7e807890-c777-466a-897).

We trust that this suggestion will be beneficial...

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

Identifying the pressed key on a mouse click event using Angular 2

I am working on an Angular 2 component that has a div element bound to a click event: <div #myDiv class="myClass" (click)="addAnnotation($event)"> </div> When the div is clicked, I want the addAnnotation code to run only if the 'a&ap ...

transforming JSON data into an array format on the fly

When I make a call to an endpoint in my C# code, it returns data structured like this: { "name":"test", "clients": { "client1": {"id": "1", "count": "41"}, "client2": {"name": "testName"}, "client3 ...

A guide on converting JSON to TypeScript objects while including calculated properties

I have a standard JSON service that returns data in a specific format. An example of the returned body looks like this: [{ x: 3, y: 5 }] I am looking to convert this data into instances of a customized TypeScript class called CustomClass: export class ...

Error: Unable to access _rawValidators property of null object

I'm currently facing an issue with formgroup and formcontrol in Angular. When I run ng serve, it's showing an error in the console. Does anyone have a solution for this? TypeError: Cannot read properties of null (reading '_rawValidators&a ...

Steps for integrating a universal loader in Angular

My implementation of a global loader is as follows: In the CoreModule: router.events.pipe( filter(x => x instanceof NavigationStart) ).subscribe(() => loaderService.show()); router.events.pipe( filter(x => x instanceof NavigationEnd || x in ...

Access the data within the nested JSON object

As I attempt to retrieve a value from a deeply nested Json object, I encounter a Parse error: The Json data I'm working with: { "MessageId": "f6774927-37cf-4608-b985-14a7d86a38f9", "Time": "2017-04-06T16:28: ...

Error in retrieving Ajax URL with incorrect .net format

Recently delving into C# tutorials to expand my skills, I encountered an issue while attempting to make an ajax call to the server using localhost. Despite my efforts, it seems like I am missing something crucial. It could be related to my folder structur ...

Is it TypeScript's return type a double arrow (Observable)?

I'm having a hard time understanding this: const loadData: (detailsStore: RecipeDetailsStore) => (source$: Observable<string>) => Observable<RecipeDetails> How should I interpret this? My understanding is: loadData is a function t ...

The requested path /releases/add cannot be located

In my CRUD application, I have a feature that allows users to create releases by adding a version and description. This is achieved through a button and input fields for the details. <button (click)="addRelease(version.value, description.value)" [d ...

Issue with C# sockets

I've been working on setting up a socket server in C# and a client in PHP. The communication flow between the client and server looks something like this: The client connects to the server The server sends a welcome message The client submits data T ...

Angular allows you to dynamically alter the background color of the currently active mat-tab using code

Greetings, I have an Angular page that consists of tabs created with Mat Tab. I successfully changed the background color of the active tab using SCSS and this is the code I used: ::ng-deep .mat-tab-label.mat-tab-label-active { background-color: var(--user ...

One way in Angular to set a validator pattern regex that matches either one rule or the other, but

Trying to implement user range matching with angular validators, like shown in the image below: https://i.stack.imgur.com/zXHn3.png The goal is to validate against one of two range options using an angular pattern validator: 1-99 or 1,2,5,6,8, but not b ...

How can I add a JavaScript-created element into a Primeng TurboTable component?

I am in the process of replacing a custom-made table with PrimeNG's turbotable. The issue I'm facing is that when I try to insert buttons into the table that call specific JavaScript functions, they end up displaying as [object HTMLInputElement] ...

Ensuring various conditions with ngIf

I've created a toggle function that updates the text of a link, but I'm struggling to handle multiple conditions. For example, *ngIf="condition1 or condition2". Below is an excerpt from my code: <a routerLink="/ads" class="tip" (click)="togg ...

What could be causing the image to vanish when combining a condition with 'linear-gradient' as the value for 'background-image'?

If the variable effect is set to true, I want to display the linear-gradient effect, otherwise only show the image. picture = 'https://i.blogs.es/7c43cd/elon-musk-no-queria-ser-ceo-e-hizo-todo-lo-posible-para-evitarlo-pero-asegura-que-sin-el-tesla-mo ...

One should refrain from loading the API in Angular when there is no data present, by utilizing the global.getData method

Check out this code snippet: loadNextBatch() { console.log('scrolldown'); this.pageIndex = this.pageIndex + 1; this.global.getData(`/conditions/latest?start=${this.pageIndex}&length=${this.pageSize}`) .pipe(take(1)).subscr ...

Error: The property ɵfac cannot be redefined within angular

core.js:27478 Uncaught TypeError: Cannot redefine property: ɵfac at Function.defineProperty (<anonymous>) at addDirectiveFactoryDef (core.js:27478:1) at compileComponent (core.js:27361:1) at ɵ3$1 (core.js:27674:93) at TypeDecora ...

How can I remove and rename a jstree node identified by its GUID value in Asp.Net Core 3.1 when a button is clicked?

Below is the code snippet for displaying Category in Razor View: $('#evts_button').on("click", function () { var instance = $('#jstree').jstree(true); instance.deselect_all(); console.log(i ...

"Sorry, there was an issue with AmStockCharts when trying to update the chart: Unable to assign a value to the '

Currently, I am using Angular 4.3.6 along with TypeScript 2.4.2 for my project. The issue that I am facing involves reading data from a socket and attempting to add it to the dataprovider. I came across an example at: While implementing a serial chart, q ...

Paper-dropdown-menu component failing to render properly in web browser

Encountering an issue with the rendered HTML for a basic paper-dropdown-menu. Instead of displaying as a styled menu, the list items are just appearing as a plain list on the page. Upon clicking the rendered paper-input component within the dropdown, the ...