对 WCF 服务的 Ajax 调用不返回数据

本文关键字:返回 数据 调用 Ajax WCF 服务 | 更新日期: 2023-09-27 18:37:20

我有一个wfc服务,它使用返回json数据的jquery ajax调用来调用。未返回任何数据或错误。但是,当我将 url 放入浏览器时,它会返回数据。例如

{"Title":"The Prestige","Year":"2006"}

这是我的服务合同

[ServiceContract]
public interface IMovies
{
    [OperationContract]
    [WebGet(UriTemplate="/movies", ResponseFormat=WebMessageFormat.Json)]
    Movie GetMovies();
}

<system.serviceModel>
<behaviors>
  <endpointBehaviors>
    <behavior name="web">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
  multipleSiteBindingsEnabled="true" />
<services>
  <service name="Movies">
    <endpoint address="" behaviorConfiguration="web"
      binding="webHttpBinding" contract="IMovies" />
  </service>
</services>

还有我的阿贾克斯电话

                $.ajax({
                type: "GET",
                url: "http://server/Service/Movies.svc/movies",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {                        
                    var movie = response.d;
                    $("#movieTitle").text(movie.Title);
                    $("#movieYearHidden").val(movie.Year);
                    $("#game").show();
                },
                error: function(response) { 
                    alert("Error retrieving movie. Please check connection."); 
                }
            });

当我打电话时,什么也没发生。请帮忙

对 WCF 服务的 Ajax 调用不返回数据

我认为您需要添加 json 端点。

<endpoint address="json" behaviorConfiguration="web"
  binding="webHttpBinding" contract="IMovies" />

我怀疑你的ajax调用没有发生。对于要从 ajax 调用的 WCF 服务,必须在端点行为中使用 enableWebScript

请检查我的应用程序中使用的以下配置。

<system.serviceModel>
    <services>
      <service name="WCF.TestWCF" behaviorConfiguration="TestWCFBehaviour">
        <endpoint address="" binding="webHttpBinding" contract="WCF.ITestWCF" behaviorConfiguration="TestWCFEndPointBehaviour"></endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="TestWCFBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="TestWCFEndPointBehaviour">
          <enableWebScript/>
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>