MS Ajax 调用 WebService 示例
1. WebServices.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebServices.aspx.cs" Inherits="WebServicesTest" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>AjaxWebServiceTest</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="../WebService/AjaxService.asmx" />
</Services>
</asp:ScriptManager>
<asp:UpdateProgress ID="UpdateProgress1" runat="server">
<ProgressTemplate>
<img src="../Images/loading.gif" />
Loading
</ProgressTemplate>
</asp:UpdateProgress>
<div>
<input id="inputName" type="text" size="20" runat="server" />
<input id="btnTest" type="button" value="调 用" onclick="return btnTest_Click()" />
</div>
</form>
</body>
</html>
<script type="text/javascript" language="JavaScript">
function $(s){return document.getElementById(s);}
function btnTest_Click()
{
var requestSimpleService = AjaxService.AjaxServiceTest(
$('<%=inputName.ClientID %>').value, //params
OnRequestComplete //Complete event
);
return false;
}
function OnRequestComplete(result)
{
alert(result);
}
</script>
2. AjaxService.asmx
<%@ WebService
Language="C#"
CodeBehind="~/App_Code/WebService/AjaxService.cs"
Class="AjaxService" %>
3. AjaxService.cs
using System;
using System.Web.Services;
using System.Web.Script.Services;//***必须
[ScriptService]
public class AjaxService : WebService
{
public AjaxService()
{ }
[WebMethod]
public string AjaxServiceTest(string s)
{
Framework.AppException.LogFile(s);
return "Hello " + s;
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebServices.aspx.cs" Inherits="WebServicesTest" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>AjaxWebServiceTest</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="../WebService/AjaxService.asmx" />
</Services>
</asp:ScriptManager>
<asp:UpdateProgress ID="UpdateProgress1" runat="server">
<ProgressTemplate>
<img src="../Images/loading.gif" />
Loading
</ProgressTemplate>
</asp:UpdateProgress>
<div>
<input id="inputName" type="text" size="20" runat="server" />
<input id="btnTest" type="button" value="调 用" onclick="return btnTest_Click()" />
</div>
</form>
</body>
</html>
<script type="text/javascript" language="JavaScript">
function $(s){return document.getElementById(s);}
function btnTest_Click()
{
var requestSimpleService = AjaxService.AjaxServiceTest(
$('<%=inputName.ClientID %>').value, //params
OnRequestComplete //Complete event
);
return false;
}
function OnRequestComplete(result)
{
alert(result);
}
</script>
2. AjaxService.asmx
<%@ WebService
Language="C#"
CodeBehind="~/App_Code/WebService/AjaxService.cs"
Class="AjaxService" %>
3. AjaxService.cs
using System;
using System.Web.Services;
using System.Web.Script.Services;//***必须
[ScriptService]
public class AjaxService : WebService
{
public AjaxService()
{ }
[WebMethod]
public string AjaxServiceTest(string s)
{
Framework.AppException.LogFile(s);
return "Hello " + s;
}
}