2016-12-13 40 views
0

我有WCF服務訪問問題。CORS WCF:對預檢請求的響應沒有通過訪問控制

這是我的WCF

[OperationContract] 
    [WebInvoke(
     RequestFormat = WebMessageFormat.Json, 
     ResponseFormat = WebMessageFormat.Json, Method = "POST", 
     UriTemplate = "/GetAllRequest/")]  
    IEnumerable<USP_GET_DATA_Result> Get(); 

然後我下面的教程tutorial CORS

這是我的服務來獲取數據

function (app) { 
 
    app.service('AngularService', ['$http', function ($http) { 
 
     return { 
 
      get: function() { 
 
       return $http({ 
 
        method: 'POST', 
 
        headers: { 
 
         'Content-Type': 'application/json; charset=utf-8' 
 
        }, 
 
        url: 'http://localhost:51458/ServiceRequest.svc/GetAllRequest/', 
 
        data: {} 
 
       }); 
 
      } 
 
     }; 
 
    }]);

然後,這是我的控制器

(function (app) { 
 
    'use strict'; 
 

 
    app.controller('entryCtrl', entryCtrl); 
 

 
    entryCtrl.$inject = ['$scope', '$http', 'AngularService']; 
 

 
    function entryCtrl($scope, $http, AngularService) { 
 
     $scope.pageClass = 'page-entry'; 
 

 
     //load data 
 
     AngularService.get().success(function (response) { 
 
      $scope.entryData = JSON.parse(response.d); 
 
     }); 
 

 
    } 
 

 
})(angular.module('entry'));

後來,當我跑我有這樣的錯誤: enter image description here

編輯:加我的web.config

<?xml version="1.0" encoding="utf-8"?> 
 
<configuration> 
 
    <configSections> 
 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
 
    </configSections> 
 
    <!--add element connectionStrings--> 
 
    <connectionStrings> 
 
    <add name="ConnectionString" connectionString="Data Source=192.168.0.202; Initial Catalog=example.KRIS; UID=sa; Password=*****" providerName="System.Data.sqlClient" /> 
 
    <add name="Entities" connectionString="metadata=res://*/DBEntities.csdl|res://*/DBEntities.ssdl|res://*/DBEntities.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=192.168.0.202;initial catalog=example.KRIS;user id=sa;password=******;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> 
 
    </connectionStrings> 
 
    <appSettings> 
 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> 
 
    </appSettings> 
 
    <system.web> 
 
    <compilation debug="true" targetFramework="4.5.2" /> 
 
    <httpRuntime targetFramework="4.5.2" /> 
 
    </system.web> 
 
    <system.serviceModel> 
 
    <!--add element extensions and services--> 
 
    <extensions> 
 
     <behaviorExtensions> 
 
     <add name="crossOriginResourceSharingBehavior" 
 
      type="WcfService.CORSEnablingBehavior, 
 
      WcfService, Version=1.0.0.0, Culture=neutral" /> 
 
     </behaviorExtensions> 
 
    </extensions> 
 
    <services> 
 
     <service behaviorConfiguration="serviceBehavior" name="WcfService.ServiceRequest"> 
 
     <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" contract="WcfService.IServiceRequest" /> 
 
     </service> 
 
    </services> 
 
    <behaviors> 
 
     <!--edit value--> 
 
     <serviceBehaviors> 
 
     <behavior name="serviceBehavior"> 
 
      <serviceMetadata httpGetEnabled="false" /> 
 
      <serviceDebug includeExceptionDetailInFaults="true" /> 
 
     </behavior> 
 
     </serviceBehaviors> 
 
     <!--add element endpoint--> 
 
     <endpointBehaviors> 
 
     <behavior name="web"> 
 
      <webHttp /> 
 
      <crossOriginResourceSharingBehavior /> 
 
     </behavior> 
 
     </endpointBehaviors> 
 
    </behaviors> 
 
    <!--edit protocolmapping--> 
 
    <protocolMapping> 
 
     <add binding="webHttpBinding" scheme="http" /> 
 
    </protocolMapping> 
 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 
 
    </system.serviceModel> 
 
    <system.webServer> 
 
    <modules runAllManagedModulesForAllRequests="true" /> 
 
    <!-- 
 
     To browse web app root directory during debugging, set the value below to true. 
 
     Set to false before deployment to avoid disclosing web app folder information. 
 
     --> 
 
    <directoryBrowse enabled="true" /> 
 
    </system.webServer> 
 
    <entityFramework> 
 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 
 
     <parameters> 
 
     <parameter value="mssqllocaldb" /> 
 
     </parameters> 
 
    </defaultConnectionFactory> 
 
    <providers> 
 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
 
    </providers> 
 
    </entityFramework> 
 
</configuration>

回答

0

將以下代碼放在global.asax中,因爲您需要允許可以稱爲客戶端的方法。

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, OPTIONS, 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(); 
    } 
} 
+0

你是否在我的項目WCF或我的應用程序中挖掘global.asax?在我的WCF項目中沒有global.asax。然後,已經把我的應用程序上面'protected void Application_Start() AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } This still error – Stfvns

+0

在你的'WCF服務global.asax'中 – Mairaj

+0

你可以在'wcf'中創建一個新的'global.asax'文件' – Mairaj

相關問題