0

我有一个 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>

阿贾克斯调用:

  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 );
      });
  }
4

1 回答 1

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 );
  });
于 2012-12-06T09:28:24.130 回答