2010-03-10 149 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

你是對的達林!事實證明,由於相同的原產地政策,這不起作用。所有這些對我來說都是新鮮事物,我不認爲會有任何問題,因爲它們在同一個盒子上運行,但由於端口號不同,所以未能進行策略驗證。