I am trying to get my Ionic app to authenticate user with ASP.NET Web Api hosted on my development PC. Here is what I’m getting in console:
XMLHttpRequest cannot load http://localhost:50669/token. 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 400.
Here is my login function:
login(data){
data.grant_type = 'password';
return this.http.post(this.baseUrl+'token', data)
.map(this.extractData);
}
private extractData(res: Response){
let body = res.json();
if (body.access_token != null){
window.localStorage.setItem('token', body.access_token);
};
return body || {};
}
I’ve checked https://www.html5rocks.com/en/tutorials/cors/ and other ionic v1 forum questions/answers - couldn’t help me much. I’ve also tried to add google chrome extension to enable Cors with the request but that gives this error in console:
response for preflight has invalid HTTP status code 400
I am new to Ionic and need your help to figure out how I can fix this. Also in my ASP app, Access-Control-Allow-Origin has * value and if I use Postman app, everything works fine. I think I need to change the header of the ionic login post request with appropriate header value. Guys, please give me some light on this. Thanks in advance.