WCF REST服务不适用于URI中的参数

时间:2012-12-06 09:19:27

标签: wcf jquery

我有一个WCF REST服务(.NET4)。

对于没有参数的调用,它可以正常工作(浏览器,ajax调用)但我无法使用参数。无论是在浏览器中还是通过ajax调用。

我的合同:

[OperationContract]
[WebGet(
    ResponseFormat = WebMessageFormat.Json,
    UriTemplate = "knowledgefields")]
IEnumerable<cKnowledgeField> GetKnowledgeFields();

[OperationContract]
[WebGet(
    BodyStyle = WebMessageBodyStyle.Bare,
    ResponseFormat = WebMessageFormat.Json,
    RequestFormat = WebMessageFormat.Json,
    UriTemplate = "knowledgeitems?id={id}")]
IEnumerable<cKnowledgeItem> GetKnowledgeItemsByField(string id);

我的web.config

<configuration>
  <appSettings/>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
      </assemblies>
    </compilation>
  </system.web>
  <system.serviceModel>
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint name="" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json"/>
      </webHttpEndpoint>
    </standardEndpoints>
    <services>
      <service name="ExpertData.expertREST" behaviorConfiguration="META">
        <endpoint address="" bindingConfiguration="webHttpBindingWithJsonP" binding="webHttpBinding" contract="ExpertData.IexpertREST"/>
      </service>
    </services>
    <protocolMapping>
      <add scheme="http" binding="webHttpBinding"/>
    </protocolMapping>
    <behaviors>
      <serviceBehaviors>
        <behavior name="META">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true"/>
      </webHttpBinding>
    </bindings>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="false" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>
  <connectionStrings>
    <add name="FindAnExpertEntities" connectionString="metadata=res://*/ExpertData.csdl|res://*/ExpertData.ssdl|res://*/ExpertData.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=WIN8ATWORK\SQLEXPRESS;initial catalog=FindAnExpert;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient"/>
  </connectionStrings>
</configuration>

Ajax电话:

  function getKnowledgeItems(id) {
      return $.ajax({
          url: "http://localhost:31634/expertREST.svc/knowledgeitems",
          dataType: "jsonp"
          data: { "id" : id + "" }
      }).then( function( data, textStatus, jqXHR ) {
          amplify.publish( "knowledgeItemsdata.updated", data );
      });
  }

1 个答案:

答案 0 :(得分:1)

你的AJAX调用是在消息体中传递ID,但是你的服务期望它作为查询字符串的一部分。将javascript更改为:

  return $.ajax({
      url: "http://localhost:31634/expertREST.svc/knowledgeitems"
           + "?id=" + id,
      dataType: "jsonp"
  }).then( function( data, textStatus, jqXHR ) {
      amplify.publish( "knowledgeItemsdata.updated", data );
  });