I'm getting this error, with ionic serve is working fine in the browser, but when i ran ionic cordova emulate android or ios is not working as expected, i tried to fetch another url and with another url is working(public api) but no with my server, i searched on the web and its look like it is a cors issue, but i cant find the solution.
headers:HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, headers: Map(0)}
message:"Http failure response for (unknown url): 0 Unknown Error"
name:"HttpErrorResponse"
ok:false
status:0
statusText:"Unknown Error"
url:null
__proto__:HttpResponseBase {constructor: }
This is my service in ionic with httpclient from angular:
return this.http.post<Response>(`${this.url}/api/account/login`, credentials, {
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Access-Control-Allow-Origin': '*'
}
})
.pipe(
tap(res => {
this.spinner.dismiss();
if (res.ResponseCode == 0) {
this.storage.set(TOKEN_KEY, res.Data[0]);
localStorage.setItem(TOKEN_KEY, res.Data[0]);
this.user = this.helper.decodeToken(res.Data[0]);
this.state.state = true;
this.state.user = this.user;
this.authenticationState.next(this.state);
} else {
this.showAlert(res.ResponseMessage);
}
}),
catchError(e => {
this.showAlert(e.error.msg);
throw new Error(e);
})
);
and this is my net core 2.1 startup for configuring the cors option;
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ApplicationDbContext dbContext)
{
app.UseCors(options => options.WithOrigins("http://localhost:8100").AllowAnyMethod().AllowAnyHeader());
}
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddDefaultPolicy(
builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
}