Ionic 2无法发布到asp dotnet核心,即使启用CORS
问题描述:
我试图寻找答案,有些接近但不是我所期待的。Ionic 2无法发布到asp dotnet核心,即使启用CORS
这是我的问题,我正在开发一个Ionic应用程序,目前正在浏览网页视图。我无法将任何内容发回我的asp dotnet核心后端。
我栈如下: 前端
- 离子2 - 使用最新版本的角4
- Nginx的最新版本 - 因为我的代理服务器
- 泊坞窗 - 我的容器中运行的dotnet核心应用
下面是这一职位的代码我想:
let body =JSON.stringify({ 'username': username, 'password': password });
let options = new RequestOptions({headers:new Headers()});
options.headers.set('Content-Type', 'application/json; charset=UTF-8');
options.headers.set('Accept', 'application/json');
return this.http.post(this.loginUrl,body,options)
.map((res: Response) => res.json())
.catch((error: any) => {
console.log("ERROR", error);
return Observable.throw(error.json().error || 'Server error')
})
我的代理有
add_header 'Access-Control-Allow-Origin' *;
我的支持在Startup.cs
options.AddPolicy("CorsPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
,并在配置功能
app.UseMvc().UseCors("CorsPolicy");
我在输出看到下面的CORS配置窗口在Visual Studio 2017中的响应正在到达容器:
Application Insights Telemetry (unconfigured): {"name":"Microsoft.ApplicationInsights.Dev.Request","time":"2017-07-15T19:29:10.5330300Z","tags":{"ai.internal.nodeName":"9a9595bd67c1","ai.operation.id":"0HL6BMO90PHKC","ai.application.ver":"1.0.0.0","ai.operation.name":"OPTIONS Users/AuthenticateUser","ai.internal.sdkVersion":"aspnet5c:2.0.0","ai.cloud.roleInstance":"9a9595bd67c1","ai.location.ip":"172.18.0.5"},"data":{"baseType":"RequestData","baseData":{"ver":2,"id":"HKmnWfByHeU=","name":"OPTIONS Users/AuthenticateUser","duration":"00:00:11.2837000","success":false,"responseCode":"415","url":"http://leo.users/users/authenticate","properties":{"DeveloperMode":"true","httpMethod":"OPTIONS","AspNetCoreEnvironment":"Development"}}}}
我尝试了多种方法在Ionic 2端设置标头。 我不知道该怎么做/如果我只是想念一些东西。帮助将非常感谢。
感谢
答
感谢misha130上面的注释。这解决了我的问题。
我刚刚更新我的离子代理文件,并添加:再次
{
"path": "/users",
"proxyUrl": "http://localhost:8080/users"
}
感谢。
那么首先你不需要cors,因为你有本地服务的代理(https://github.com/ionic-team/ionic-proxy-example/blob/master/ionic.project)和手机根本不需要Cors,因为它不使用这些策略。如果你真的想知道为什么你的CORS不工作,你可以看看你的请求/响应标题,看看所有的标题是否存在 – misha130
感谢您的答复。我必须有CORS。我正在设计一个微服务架构。目前这意味着我的代理正在一个docker容器中运行,另外两个微服务运行在不同的cotainer中。正因为如此,我从我的代理服务器访问我的服务的方式就像'http:// service.name'。我意识到,为了在移动设备上运行,我不会遇到这些问题。但现在我正在使用我的网络浏览器进行开发,并且需要找到解决这个问题的方法。因为我将在angular 2中开发一个网页,我假设我将面临同样的问题。 –