user6021893
user6021893

Reputation:

Typescript (ionic app) - Basic auth not working

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.

Upvotes: 0

Views: 1761

Answers (2)

Christo Goosen
Christo Goosen

Reputation: 586

Check that your API has CORS setup. Your API needs a options method for CORS preflight check. https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS, https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request.

Upvotes: 0

slashpm
slashpm

Reputation: 214

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)

Upvotes: 0

Related Questions