Service.ts
public welcome(token: any){
let tokenString = "Bearer "+token
console.log("tokenString is: "+tokenString)
let header = new HttpHeaders().set("Authorization",tokenString);
const requestOptions = { headers: header};
return this.httpClient.get('http://localhost:8191/api/',{
responseType: 'text' as 'json',
headers: header
});
}
WebPage Console:
tokenString is: Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJqYXZhdGVjaGllIiwiZXhwIjoxNjIzMTMyNzc5LCJpYXQiOjE2MjMwOTY3Nzl9.h6aw8VBFHXWJQ5R2jRyn0MUqbe4rT3RvUCsELfcKHSU
Access to XMLHttpRequest at 'http://localhost:8191/api/' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, headers: Map(0)} message: "Http failure response for http://localhost:8191/api/: 0 Unknown Error" name: "HttpErrorResponse" ok: false status: 0 statusText: "Unknown Error" url: "http://localhost:8191/api/"
Controller Postman request is working enter image description here
http://localhost:8191/api/
, have you enabled CORS on that API/server? – Alexander Staroselsky