新建立一个web Site ,然后在这个Site中新加一个ajax-enable WCF Service  ,如下图 名为Service

Ajax 和 WCF 的简单运用

建立后在AppCode下面就有一个CS文件 ,另外在根目录下面有一个SVC文件,此文件的指向的后台文件就是AppCode下的CS文件

%>

打开CS文件,对代码进行如下修改

System; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Activation; using System.ServiceModel.Web; [ServiceContract(Namespace = "MyNameSpace")]//这里定义Class所有命名空间 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class Service { [OperationContract] //暴露一个方法 public string getMessage() { return "WCF服务器返回成功!!"; } }

以上对WCF设置部分完成(其实在Web.config新加了配置system.serviceModel空间的配置信息)

下面在ASPX页面调用 :

拉一个ScriptManager ,在里面加一WCF服务的路径,代码如下

<Services > <asp:ServiceReference Path="~/Service.svc" /> 指定SVC文件的路径 </Services> </asp:ScriptManager>

在同一个页面的JS代码如下

function ajaxTest() { MyNameSpace.Service.getMessage(onMethodCompleted); //这里有好几个重载了几个方法,分别用来指明成功或失败时的回调函数 } function onMethodCompleted(results) { var inner = document.getElementById("Show"); inner.innerText = results; } </script>

在aspx页面拉一个客户端按钮 ,指定onclick事件为ajaxTest  这样当点击按钮时, WCF那方法的返回值将显示出来...

相关文章:

  • 2021-09-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-31
  • 2021-09-18
猜你喜欢
  • 2021-11-22
  • 2021-09-28
  • 2021-10-11
  • 2022-02-07
  • 2021-08-03
  • 2022-01-31
  • 2022-12-23
相关资源
相似解决方案