Ajax调用WCF服务而不返回数据

时间:2013-10-29 08:52:32

标签: c# asp.net wcf web-services

我有一个使用jquery调用的wfc服务ajax调用taht返回json数据。没有返回任何数据或错误。当我将url放入浏览器时,它会返回数据。 e.g

{"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电话

                $.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."); 
                }

            });

当我打电话时没有任何反应。请帮忙

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

我怀疑你的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>