ASP.NET AJAX调用服务

之前写程序的时候,在调用服务(web services和WCF)的时候,都是在项目中用web引用(web services )和服务引用(WCF)的方法,然后在后台直接实例化,这样就可以使用服务中的方法了。

今天看到一篇关于asp.net ajax调用服务的文章,就没事研究了一下。这里AJAX主要指的是ScriptManager控件。

在ScriptManager控件中提供了Services子元素,这个子元素有提供了ServiceReference摸板以实现对服务的引用。

1.ScriptManager引用web services:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ScriptManager控件调用WCF._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
         <Scripts>
            <asp:ScriptReference Path="~/main.js" />
         </Scripts>
         <Services>
             <asp:ServiceReference Path="MyWebService.asmx"/>
         </Services>
       </asp:ScriptManager>
       <input id="Text1" type="text" value="rock"/>
       <input id="Button1" type="button" value="button" onclick="myalert();" />
    
    </div>
    </form>
</body>
</html>

其中Scripts子元素提供了ScriptReference摸板,是对脚本文件的调用。

2.ScriptManager引用WCF服务,引用方法和Web services是一样的。这里需要注意的是,这里引用的不是普通的WCF服务,而是“启用了AJAX的WCF服务”,否则后面是找不到普通的WCF服务的

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ScriptManager控件调用WCF._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
         <Scripts>
            <asp:ScriptReference Path="~/main.js" />
         </Scripts>
         <Services>
            <asp:ServiceReference Path="~/Service1.svc"/>
         </Services>
       </asp:ScriptManager>
       <input id="Text1" type="text" value="rock"/>
       <input id="Button1" type="button" value="button" onclick="myalert();" />
    
    </div>
    </form>
</body>
</html>

3.web services mian.js文件是用来实现事件的

//Web services
function myalert() {
   //引用web services时,要添加它的命名空间,否则回报“MyWebService未定义”
   ajax控件使用.MyWebService.HelloSomeBody(
                  document.getElementById("Text1").value,  //参数
                  WebServiceSucceededMethod,   //WebService调用成功时回调的函数,可添加也可不添加
                  WebServiceFailedMethod  //WebService调用失败时回调的函数,可添加也可不添加
         );
   return false;

}


//WebService调用成功时回调的函数
function WebServiceSucceededMethod(result) {
   alert("执行成功的方法\r\n" + result);

}
//WebService调用失败时回调的函数
function WebServiceFailedMethod(result) {
   alert("执行失败的方法\r\n" + result);
}



function complete(result) {
   alert(result);
}
    

这里的调用web services时必须加上命名空间( ajax控件使用)即 ajax控件使用.MyWebService,否则会报“MyWebService”未定义。

4.WCF 的main.js

function myalert() {
   //这里的添加的是“启用了AJAX的WCF服务”,不是普通的WCF服务
   var wcfProxy = new Service1();
   wcfProxy.HelloSomeBoby(
                   document.getElementById("Text1").value,  //参数
                   WebServiceSucceededMethod,   //WebService调用成功时回调的函数,可添加也可不添加
                   WebServiceFailedMethod  //WebService调用失败时回调的函数,可添加也可不添加
                 //  complete  //完成事件
         );
   return false;

}
//WebService调用成功时回调的函数
function WebServiceSucceededMethod(result) {
   alert("执行成功的方法\r\n" + result);
  
}
//WebService调用失败时回调的函数
function WebServiceFailedMethod(result) {
   alert("执行失败的方法\r\n" + result);
}


function complete(result) {
   alert(result);
}
    

这里可以直接调用Service1,不需要加命名空间

5.最后实现的效果如下:

这就是asp.net ajax调用服务

这里省略了实现服务的过程,大家可以先学习服务,然后在进一步学习这里。

还有一个需要说明的地方,就是目前我这里只实现了同一个项目中调用服务,我看了网上说,目前还不能实现引用外部的服务,

对于引用外部的,不知大家是否有方法实现。

如果需要源代码的,可以留言给我。

原文地址:https://www.cnblogs.com/love828/p/2610282.html