在WCF Restfull服务

时间:2015-09-29 04:35:32

标签: c# jquery asp.net ajax wcf

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

在WCFService中。

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>

1 个答案:

答案 0 :(得分:0)

请将该属性添加到实现服务IProductService

的类中
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]