2017-11-08 9 views
0

私は、コードの角度側はこの `XML形式での角度でWCFサービスを消費

mainApp.factory('MediaService', ['$http', function ($http) { 

    var factory = {}; 
    factory.getMediaLoops = function() { 

     return $http.post("http://localhost:62467/MediaLoopService.MediaLoop.svc/GetMediaLoops"); 
    } 
    return factory;}]); 
のように見えるこの`

[WebInvoke(Method = "POST", 
    BodyStyle = WebMessageBodyStyle.Wrapped, 
    ResponseFormat = WebMessageFormat.Xml)] 
     public string GetMediaLoops() 
     { 
      string strFileName = "GetMediaLoops_response.xml"; 
      string strResult = GetFileData(strFileName); 
      return strResult; 
     }` 

のように見える機能GetMediaLoopsを持つMediaServiceLoop.svcファイルを持っています

Controller code:

MediaService.getMediaLoops() 
      .then(function (response) { 
       $scope.media = response.data; 
      }, function (error) { 
       $scope.status = 'Unable to load customer data: ' + error.message; 
      }); 

`

私はPostmanとChrome Network(Network Image)の両方で回答を得ることができましたが、「No」Access-Control-Allow-Originヘッダが要求されたリソースに存在します。 Origin 'http://localhost:2206'はアクセスが許可されていないため、コードフローはgetMediaLoops角度呼び出しの応答とエラーブロックをスキップします。

また、上記のエラーのために以下のコードをGlobal.asaxファイルに追加しました上記のエラーを取得し、すべてのヘルプは

protected void Application_BeginRequest(object sender, EventArgs e) 
     { 
      HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "http://localhost"); 
      if (HttpContext.Current.Request.HttpMethod == "OPTIONS") 
      { 
       HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, PUT, DELETE"); 

       HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept"); 
       HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000"); 
       HttpContext.Current.Response.End(); 
      } 

     } 

答えて

1

をいただければ幸いサービスのweb.configファイルに以下の設定を追加してください:

<system.webServer> 
    <httpProtocol> 
     <customHeaders> 
      <add name="Access-Control-Allow-Origin" value="*" /> 
     </customHeaders> 
    </httpProtocol>  

    <modules runAllManagedModulesForAllRequests="true"/> 
    <directoryBrowse enabled="true"/> 
</system.webServer> 
+0

ありがとう、魅力的なように働いた:) –

0

APIにはのparamsを渡すない時には、上記の答えはうまく働いたが、しかし、データを渡すためWCFはブラウザで作成されたプリフライトリクエストの一部としてOPTIONSリクエストに応答せず、エラー "Method not allowed:405"を投げたので、永続的な解決策は、サービスでグローバルファイルを作成し、OPTIONS要求をチェックしてヘッダーを追加することでしたしたがって、

public class Global : System.Web.HttpApplication 
{ 

    protected void Application_BeginRequest(object sender, EventArgs e) 
    { 
     HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); 
     if (HttpContext.Current.Request.HttpMethod == "OPTIONS") 
     { 
      HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE"); 
      HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept"); 
      HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000"); 
      HttpContext.Current.Response.End(); 
     } 
    } 

} 
関連する問題