asp.net-core - ionic httpclient 在带有 net core 0 Unknown Error 的模拟器上不起作用”
问题描述
我收到此错误,离子服务在浏览器中工作正常,但是当我运行 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();
});
});
}
解决方案
推荐阅读
- nativescript - Nativescript - WebView / HtmlView + 3rd 方 javascript
- html - Selectors for divs with specific data attributes
- r - 在 R 中,ts.plot( gpars col=c("red","blue") 只返回一个彩色图)
- vue.js - 是否有 Vue.js 表单验证库在模糊时显示错误,修复后立即隐藏错误
- extjs - 如何在 ExtJS Modern 中的控制框周围设置边框?
- java - java JList不会刷新,在调试器中工作
- verilog - 无法在 verilog 中详细说明实例化模块
- sql-server-2012 - 有条件地从一张表中汇总一列中一组的值
- python - 尝试使用给定列表更新年龄值
- pointers - 为什么使用不安全代码的二叉树在调试模式下有错误的内存访问,但没有释放?