views:

208

answers:

3

I have a problem with getting jquery to retrieve results from a WCF service. I am hosting the WCF service within IIS and when I attach the debugger to this process I can see that the code is processed successfully. However, when it hits the callback within jquery there is no data??

I have set up a trace on the wcf service and there are no errors. It just seems as though the data is lost after the wcf service method completes.

Here is the jquery code which calls the service:

$.get("http://ecopssvc:6970/ecopsService.svc/Echo", {echoThis: "please work"}, function(data) {
            alert("Data Loaded: " + data);
        });

Here is the wcf config:

    <system.serviceModel>
    <services>
      <service name="EcopsWebServices.EcopsService" behaviorConfiguration="EcopsServiceBehaviours">
        <endpoint address="" behaviorConfiguration="WebBehaviour"
          binding="webHttpBinding" bindingConfiguration="" contract="EcopsWebServices.IEcopsServiceContract" />
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="WebBehaviour">
          <webHttp />
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="EcopsServiceBehaviours">
          <serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

Here is the service contract:

    [ServiceContract]
public interface IEcopsServiceContract
{    
    [WebGet]
    [OperationContract]
    string Echo(string echoThis);

}

Here is the Service implementation:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class EcopsService : IEcopsServiceContract
    {

        #region IEcopsServiceContract Members

        public string Echo(string echoThis)
        {
            return string.Format("You sent this '{0}'.", echoThis);
        }

        #endregion
    }

Please help someone!!!

A: 

I would do 2 things:

  • use debugging tools on the browser side - either the IE8 built-in debugger, or Firebug for Firefox. Use this to examine what you send to WCF, and what you get back.

  • Use Fiddler to examine the HTTP requests and responses as they flow across the wire.

Cheeso
I checked out the response using fiddler and it seems that I am receiving a response back. Please note, I have been tinkering with the code to return json format:HTTP/1.1 200 OKContent-Type: application/json; charset=utf-8Server: Microsoft-IIS/7.0X-Powered-By: ASP.NETDate: Wed, 10 Mar 2010 13:02:23 GMTContent-Length: 36{"d":"You sent this 'please work'."}
smithy
ok, so you've got a respnse in the browser. At this point you have to look to see if you are properly handling that response. I think you need to call eval() on it, if you want an object with those properties to be created in the context of the browser. eg `var obj = eval(response);`
Cheeso
If you use jQuery, it would be something like `var obj = $.parseJSON(response);`
Cheeso
+2  A: 

Make sure to set the factory to WebScriptServiceHostFactory in your .svc markup. Also make sure that the WCF service and the web page respect the same origin policy. Here's a full working example:

WCF Service:

[ServiceContract]
public interface IService1
{
    [WebGet]
    [OperationContract]
    string Echo(string echoThis);
}

public class Service1 : IService1
{
    public string Echo(string echoThis)
    {
        return string.Format("You sent this '{0}'.", echoThis);
    }
}

web.config:

<system.serviceModel>
  <services>
    <service name="ToDD.Service1" 
             behaviorConfiguration="ToDD.Service1Behavior">
      <endpoint address="" 
                binding="webHttpBinding" 
                contract="ToDD.IService1" />
      <endpoint address="mex" 
                binding="mexHttpBinding" 
                contract="IMetadataExchange"/>
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="ToDD.Service1Behavior">
        <serviceMetadata httpGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="false"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

service1.svc:

<%@ ServiceHost 
    Language="C#" 
    Debug="true" 
    Service="ToDD.Service1" 
    CodeBehind="Service1.svc.cs" 
    Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"
%>

index.htm:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Test</title>
    <script type="text/javascript" src="jquery-1.4.1.js"></script>
    <script type="text/javascript">
    $(function() {
        $.getJSON('http://localhost:4660/service1.svc/Echo', { 
            echoThis: 'please work' }, 
            function(data) {
                alert(data.d);
            }
        );
    });
    </script>
</head>
<body>
</body>
</html>
Darin Dimitrov
A: 

You were right Darin! It turns out that it wasn't working because of the same origin policy. All this stuff is new to me and I didn't think there would be any problems since they were running on the same box but because the port number was different they failed the policy validation.

Ben Smith