Reputation: 63
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!!!
Upvotes: 3
Views: 1674
Reputation: 1
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.
Upvotes: 0
Reputation: 1039418
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">
<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>
Upvotes: 2
Reputation: 192627
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.
Upvotes: 0