【问题标题】:Permission denied to access property "apply" in WCF Restfull services在 WCF Restful 服务中访问属性“应用”的权限被拒绝
【发布时间】:2015-12-26 10:46:54
【问题描述】:

在我的解决方案文件中,我添加了两个项目,一个是 WCFService 应用程序,另一个是空的 MVC 应用程序。

在 WCF 服务中。

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

WCF 配置文件如下

<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 时,在浏览器控制台中出现此错误。

**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>

【问题讨论】:

  • 为什么不在 MVC 项目中添加服务引用?
  • 它是WCF restfull服务,那么为什么我需要在我的Web中引用WCF,什么时候可以使用ajax调用它。

标签: c# jquery asp.net ajax wcf


【解决方案1】:

请将该属性添加到实现您的服务 IProductService 的类中

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

【讨论】:

  • 谢谢它的工作。但是还有另一种方法可以解决这个问题。在 ajax 请求中添加 crossDomain: true,。它也可以工作
  • OK 如果它完全满足您的要求,然后标记并关闭问题,这将对其他导师和观众有所帮助。
猜你喜欢
  • 2012-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-05
  • 2015-03-18
  • 2016-09-02
  • 2016-05-02
相关资源
最近更新 更多