I'm using Java Rest API services remotely. They are sending username & password along with services for authorization. Here i'm using angular6 and there is problem with the headers i'm passing. Below is my code
import { Injectable } from '@angular/core';
import { HttpClient,HttpHeaders} from '@angular/common/http';
import { Http, Response } from '@angular/http';
import { Observable, Subject } from 'rxjs';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import { HttpModule } from '@angular/http';
`
@Injectable()
export class MasterService{
constructor(private http:HttpClient) { }
getApi(url): Observable<any> {
let headers = new HttpHeaders();
headers = headers.append('Content-Type', 'application/json');
headers = headers.append("Authorization", "Basic YWRtaW46YWRtaW4=");
headers = headers.append('Access-Control-Allow-Origin', '*');
headers = headers.append('UserName', 'admin');
headers = headers.append('password', 'admin');
return this.http.get(url,{ headers:headers })
.map((res) => {
console.log(res);
return res;
}).catch(this.handleErrorObservable);
}
}
`
I got error like this
OPTIONS http://publicIp/coms/getEnterpriseCutomerDtls?custid=10 401
(Unauthorized)
Access to XMLHttpRequest at 'http://publicIp/coms/getEnterpriseCutomerDtls?
custid=10' from origin 'http://localhost:4200' has been blocked by
CORSpolicy: Response to preflight request doesn't pass access control check:
No 'Access-Control-Allow-Origin' header is present on the requested
resource.
Thanks In Advance