Hi All,
Just come across this thread. I think it addresses the problem I'm having.
I have a web api service built with Asp.Net Core that I am trying to access
from an Angular 2 app. The web api is set to allow anonymous access, and I
have enabled CORS, but am getting a 401 when I try to call it. Following
the code snippets, here, I did the following:
- Created a file CORSBrowserXhr.ts and added the CORSBrowserXhr
definition in it
- Modified my application bootstrap file to include the additional
bootstrapping parameters
I'm still getting a 401 though, and I can see in the browser's network
request pane that there are no headers being added. Any thoughts on what
I've missed? Here are the files in question:
CorsBroweserChr.ts
import { Injectable} from '@angular/core';
import {BrowserXhr} from '@angular/http'
@Injectable()
export class CORSBrowserXHr extends BrowserXhr {
build(): any {
var x:any = super.build();
x['withCredentials'] = true;
return x;
}
}
main.ts
import { bootstrap } from '@angular/platform-browser-dynamic';
import { AppComponent } from './app.component';
import {HTTP_PROVIDERS, BrowserXhr} from '@angular/http';
import {CORSBrowserXHr} from "./CORSBrowserXHr";
import {provide} from "@angular/core";
bootstrap(AppComponent,
[
HTTP_PROVIDERS,
provide(BrowserXhr, { useClass: CORSBrowserXHr }),
]);
and here is where I make my call:
import { Injectable} from '@angular/core';
import {RiskListSummary} from '../Models/RiskListSummary';
import { Observable } from 'rxjs/Rx';
import { Http, Response } from '@angular/http';
@Injectable()
export class RiskAssessmentListService {
constructor(private http : Http) {}
getRisks(): Observable<RiskListSummary[]> {
return this.http.get(this.serviceUrl)
.map(this.extractData)
.catch(this.handleError);
}
Quite new to this, so I'm not sure where to start :-(
Thanks,
Kev
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.