我在获取jquery以从WCF服务检索结果时遇到问题。我在IIS中托管WCF服务,当我将调试器附加到此过程时,我可以看到代码已成功处理。但是,当它在jquery中遇到回调时,没有数据?
我已经在wcf服务上设置了跟踪,没有错误。看起来好像wcf服务方法完成后数据丢失了。
以下是调用服务的jquery代码:
$.get("http://ecopssvc:6970/ecopsService.svc/Echo", {echoThis: "please work"}, function(data) {
alert("Data Loaded: " + data);
});
这是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>
这是服务合同:
[ServiceContract]
public interface IEcopsServiceContract
{
[WebGet]
[OperationContract]
string Echo(string echoThis);
}
以下是服务实施:
[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
}
请帮助别人!!!
答案 0 :(得分:2)
确保在.svc
标记中将工厂设置为WebScriptServiceHostFactory。还要确保WCF服务和网页尊重same origin policy。这是一个完整的工作示例:
WCF服务:
[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>
答案 1 :(得分:0)
我会做两件事:
在浏览器端使用调试工具 - IE8内置调试器或Firebug for Firefox。用它来检查你发送给WCF的内容,以及你得到的内容。
使用Fiddler检查HTTP请求和响应,因为它们在线路上流动。
答案 2 :(得分:0)
你是对的Darin!事实证明,由于相同的原始政策,它无法正常工作。所有这些对我来说都是新的,我不认为会有任何问题,因为它们在同一个盒子上运行,但由于端口号不同,他们未能通过策略验证。