简体   繁体   中英

Ajax call to WCF service not returning data

I have a wfc service which is being called using a jquery ajax call taht returns json data. No data or error is being returned. When i put the url into the browser however it returns data. eg

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

This is my service contract

[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>

And my ajax call

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

            });

When i make the call nothing happens. Please help

I think you need to add json endpoint.

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

I doubt your ajax call is not happening. For a WCF service to be called from ajax you have to use enableWebScript in your end point behaviour.

Pls check the below configuration used in my application.

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM