问候语!
我是WCF的新手,我认为它与ASP.NET web服务类似,但我无法从客户端JavaScript调用方法。我的网页表单如下所示:
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Path="~/test.js" />
</Scripts>
<Services>
<asp:ServiceReference Path="~/MyService.svc" />
</Services>
</asp:ScriptManager>
</div>
<button onclick="test()">Click Me</button>
</form>
我的服务界面如下所示:
namespace Test
{
[ServiceContract(Namespace = "Test")]
public interface IMyService
{
[OperationContract]
void DoWork();
[OperationContract]
string SayHi();
}
}
下面是它的实现:
namespace Test
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService : IMyService
{
public void DoWork()
{
}
public string SayHi()
{
return "Hello, World!";
}
}
}
最后是JavaScript:
function test() {
Test.MyService.SayHi(SayHiSuccess, SayHiError);
}
function SayHiSuccess(result) {
alert(result[0]);
}
function SayHiError(error) {
alert(error.toString());
}
服务的SayHi()方法似乎从未执行过,尽管我不确定为什么或如何进行故障排除。有什么建议吗?