c# - WCF 服务不向 jQuery 返回值

标签 c# asp.net jquery ajax wcf

我在使用 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>

这是服务契约(Contract):

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

请帮助某人!!!

最佳答案

确保将出厂设置为 WebScriptServiceHostFactory在您的 .svc 标记中。还要确保 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);
    }
}

网络配置:

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

服务1.svc:

<%@ ServiceHost 
    Language="C#" 
    Debug="true" 
    Service="ToDD.Service1" 
    CodeBehind="Service1.svc.cs" 
    Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"
%>

索引.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>

关于c# - WCF 服务不向 jQuery 返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2416710/

相关文章:

c# - 主视图模型中的绑定(bind)

表单 runat=server 中的 Javascript 验证

javascript - jsrender 模板不会渲染,有什么想法吗?

javascript - 错误 : this. 样式未定义?

javascript - PhoneGap 中的 Cookie header : Refused to set unsafe header "Cookie"

c# - MSSQL : Can a C# function implement a view?

c# - 强制按值传递引用类型(或 .Clone()d on Pass)

c# - 找不到存储过程 'dbo.aspnet_UsersInRoles_GetRolesForUser'

c# - 使用 XmlNode 读取嵌套的 XML

c# - 使用 Blazor 渲染未知深度的树结构