aspx页面代码
<asp:ScriptManager ID="ScriptManager1" runat="server" ScriptMode="Debug">
<Services>
<asp:ServiceReference Path="WebServiceFoundation.asmx" />
</Services>
</asp:ScriptManager>
<input type="button" value="Get Random" οnclick="getRandom()" />
<input type="button" value="Get Range Random" οnclick="getRandom(50, 100)" />
<script language="javascript" type="text/javascript">
function getRandom(minValue, maxValue)
{
if (arguments.length != 2)
{
Sample.WebServiceFoundation.GetRandom(getRandomSucceeded);
}
else
{
Sample.WebServiceFoundation.GetRangeRandom(minValue, maxValue, getRandomSucceeded);
}
}
function getRandomSucceeded(result)
{
alert(result);
}
</script>
WebServiceFoundation.asmx页面代码
<%@ WebService Language="C#" Class="Sample.WebServiceFoundation" %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
namespace Sample
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]//必须要添加的
public class WebServiceFoundation : System.Web.Services.WebService
{
[WebMethod]//每个方法所需的标记
public int GetRandom()
{
return new Random(DateTime.Now.Millisecond).Next();
}
[WebMethod]
public int GetRangeRandom(int minValue, int maxValue)
{
return new Random(DateTime.Now.Millisecond).Next(minValue, maxValue);
}
}
}