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>

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 );
      });
  }
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 に答える