0
votes

Here is my code :

onClick() {
console.log("I am running...");

let headers: Headers = new Headers();
headers.append('Authorization', 'Basic ' + btoa("Userid:password") );
headers.append("Content-Type", "application/json");

let options = new RequestOptions({ headers: headers });
return this.http.get(this.url, options)
  .map((response: Response) => {
    console.log(response);
  }).subscribe();
}

I am getting error : 401 (Unauthorized)

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access. The response had HTTP status code 401.

Kindly provide me some solutions.

2
Its a CORS issue.. you have to setup appropriate headers in the server side. Also is this ionic serve? - Suraj Rao
I am getting response in postman after setting username and password but getting error here - user6021893

2 Answers

0
votes

It is a CORS issue. Make sure to have this header on your side AND on the server side too:

headers.append('Access-Control-Allow-Origin' , '*');

(Of course you can specify what you want to allow, the line above is just allowing everything for you)

0
votes