2017-07-15 73 views
3

我試圖尋找答案,有些接近但不是我所期待的。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端設置標頭。 我不知道該怎麼做/如果我只是想念一些東西。幫助將非常感謝。

感謝

+1

那麼首先你不需要cors,因爲你有本地服務的代理(https://github.com/ionic-team/ionic-proxy-example/blob/master/ionic.project)和手機根本不需要Cors,因爲它不使用這些策略。如果你真的想知道爲什麼你的CORS不工作,你可以看看你的請求/響應標題,看看所有的標題是否存在 – misha130

+0

感謝您的答覆。我必須有CORS。我正在設計一個微服務架構。目前這意味着我的代理正在一個docker容器中運行,另外兩個微服務運行在不同的cotainer中。正因爲如此,我從我的代理服務器訪問我的服務的方式就像'http:// service.name'。我意識到,爲了在移動設備上運行,我不會遇到這些問題。但現在我正在使用我的網絡瀏覽器進行開發,並且需要找到解決這個問題的方法。因爲我將在angular 2中開發一個網頁,我假設我將面臨同樣的問題。 –

回答

1

感謝misha130上面的註釋。這解決了我的問題。

我剛剛更新我的離子代理文件,並添加:再次

{ 
     "path": "/users", 
     "proxyUrl": "http://localhost:8080/users" 
    } 

感謝。

相關問題