gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:23:03 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com