2010-03-10 152 views
3

我有一个问题,让jQuery从WCF服务检索结果。我在IIS中托管WCF服务,当我将调试器附加到此过程中时,我可以看到代码已成功处理。但是,当它在jQuery中触发回调没有数据?WCF服务没有返回值到jQuery

我已经在wcf服务上设置了跟踪并且没有错误。它看起来好像在wcf服务方法完成后数据丢失了。

这里是调用服务的jQuery代码:

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

这里是WCF配置:

<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

您是否在页面上使用ASP.Net并通过''元素引用服务? – 2010-03-10 12:27:28

+2

您是否监控网络流量? (也许使用Fiddler或FireBug)..只需要找出失败点。 – Noel 2010-03-10 12:29:51

+0

感谢您的回应家伙。我没有使用元素。我不相信这是必要的。我发现这个网页http://dotnetdiscoveries.blogspot.com/2008/05/using-jquery-with-json-enabled-wcf。在我搜索谷歌期间,他已经实施它和我一样,除了他的版本的作品?! 诺埃尔,我用Firebug和我收到一个空回应...... – smithy 2010-03-10 12:50:34

回答

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

我会做两两件事:

  • 使用调试工具在浏览器端 - 无论是IE8内置的调试器,或Firefox的Firebug。使用它来检查您发送给WCF的内容,以及返回的内容。

  • 使用Fiddler检查HTTP请求和响应,当它们流过线路时。

+0

我检查了使用提琴手的响应,似乎我收到了回应。请注意,我一直在修补返回json格式的代码: HTTP/1.1 200 OK Content-Type:application/json;字符集= UTF-8 服务器:Microsoft-IIS/7.0 X供电,通过:ASP.NET 日期:星期三,2010 3月10日13时02分23秒GMT 的Content-Length:36 { “d”: “你发送了这个'请工作'。”} – smithy 2010-03-10 13:10:40

+0

好的,所以你已经在浏览器中得到了响应。此时您必须查看是否正确处理了该回复。我想你需要在它上面调用eval(),如果你想在浏览器上下文中创建一个具有这些属性的对象。例如'var obj = eval(response);' – Cheeso 2010-03-10 22:28:32

+0

如果你使用jQuery,它会像'var obj = $ .parseJSON(response);' – Cheeso 2010-03-10 22:34:23

0

你是对的达林!事实证明,由于相同的原产地政策,这不起作用。所有这些对我来说都是新鲜事物,我不认为会有任何问题,因为它们在同一个盒子上运行,但由于端口号不同,所以未能进行策略验证。