0

我收到此错误,离子服务在浏览器中工作正常,但是当我运行 ionic cordova emulate android 或 ios 没有按预期工作时,我尝试获取另一个 url 并且另一个 url 正在工作(公共 api)但是我的服务器没有,我在网上搜索,它看起来像是一个 cors 问题,但我找不到解决方案。

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: }

这是我在 ionic 中使用 Angular 提供的 httpclient 服务:

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);
    })
  );

这是我用于配置 cors 选项的 net core 2.1 启动;

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();
             });
        });
}
4

0 回答 0