0

我有一个 wfc 服务,它正在使用 jquery ajax 调用 taht 调用返回 json 数据。没有返回数据或错误。当我将网址放入浏览器时,它会返回数据。例如

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

            });

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

4

2 回答 2

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>
于 2013-10-29T09:58:12.517 回答
0

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

<endpoint address="json" behaviorConfiguration="web"
  binding="webHttpBinding" contract="IMovies" />
于 2013-10-29T09:07:15.683 回答