繁体   English   中英

WCF服务没有将值返回给jQuery

[英]WCF service not returning value to jQuery

我有一个问题,让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配置:

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

请帮助别人!!!

确保在.svc标记中将工厂设置为WebScriptServiceHostFactory 还要确保WCF服务和网页遵循相同的源策略 这是一个完整的工作示例:

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>

我会做两件事:

  • 在浏览器端使用调试工具 - IE8内置调试器或Firebug for Firefox。 使用它来检查您发送给WCF的内容以及您获得的内容。

  • 使用Fiddler检查HTTP请求和响应,因为它们在线路上流动。

你是对的Darin! 事实证明,由于相同的原始政策,它无法正常工作。 所有这些对我来说都是新的,我不认为会有任何问题,因为他们在同一个盒子上运行,但由于端口号不同,他们未能通过策略验证。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM