2015-09-29 99 views
0

在我的解決方案文件中,我添加了兩個項目,一個是WCFService應用程序和其他空的MVC應用程序。權限被拒絕在WCF Restfull服務中訪問屬性「apply」

在WCFService中。

public interface IProductService 
    { 
     [OperationContract] 
     [WebInvoke(Method = "GET", UriTemplate = "/ProductName/{productID}", 
      BodyStyle = WebMessageBodyStyle.Bare, 
      RequestFormat = WebMessageFormat.Json, 
      ResponseFormat = WebMessageFormat.Json)] 
     string GetProductName(string productID); 
    } 

,併爲後續

<system.serviceModel> 
    <services> 
     <service behaviorConfiguration="Default" 
       name="RESTFulWCFService.ProductService"> 
     <endpoint address="" behaviorConfiguration="webBehavior" 
        binding="webHttpBinding" contract="RESTFulWCFService.IProductService"></endpoint> 
     <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"></endpoint> 
     </service> 
    </services> 

    <behaviors> 
     <endpointBehaviors> 
     <behavior name="webBehavior"> 
      <webHttp helpEnabled="true"/> 
     </behavior> 
     </endpointBehaviors> 
     <serviceBehaviors> 
     <behavior name="Default"> 
      <!-- To avoid disclosing metadata information, set the values below to false before deployment --> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors>   
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    <httpProtocol> 
     <customHeaders> 
     <add name="Access-Control-Allow-Origin" value="*" /> 
     <add name="Access-Control-Allow-Methods" value="*" /> 
     <add name="Access-Control-Allow-Headers" value="*" /> 
     </customHeaders> 
    </httpProtocol> 
    <directoryBrowse enabled="true"/> 
    </system.webServer> 

現在,當我使用jQuery那麼在瀏覽器控制檯我得到這個錯誤打電話給我的WCF WCF的配置文件。

**Error: Permission denied to access property "apply"** 

我的jQuery代碼

<script> 
    $(document).ready(function() { 
     $.ajax({ 
      type: "GET", 
      url: "http://localhost:55827/ProductService.svc/ProductName/2", 
      contentType: "application/json; charset=utf-8", 
      dataType: "jsonp", 
      success: function (result) { 
       debugger; 
       alert(result.d); 
       console.log(result); 
      }, 
      error: function (result) { 
       debugger; 
       alert('no'); 
      } 
     }); 
    }); 
</script> 
+0

你爲什麼不以你的MVC項目中添加服務引用類的? –

+0

這是WCF Restfull服務,那麼爲什麼我需要在我的Web中使用WCF引用,何時可以使用Ajax調用它。 –

回答

0

請添加屬性到誰正在實施服務IProductService

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] 
+0

感謝它的工作。但還有另一種解決方法。在ajax請求中添加'crossDomain:true,'。它也將起作用 –

+0

好的如果它完全填滿了你的請求,那麼標記並關閉這個問題,這對其他指導者和觀察者會有幫助。 –

相關問題