1,使用Htttp Get 访问方法
简单例子:
WebService代码: ?
-
using?System;
-
using?System.Collections;
-
using?System.ComponentModel;
-
using?System.Data;
-
using?System.Web;
-
using?System.Web.Services;
-
using?System.Web.Services.Protocols;
-
namespace?AJAXEnabledWebApplication4
- {
-
????
-
????
-
????
-
????[WebService(Namespace?=?"http://tempuri.org/")]
- ????[WebServiceBinding(ConformsTo?=?WsiProfiles.BasicProfile1_1)]
-
????[ToolboxItem(false)]
- ????[System.Web.Script.Services.ScriptService]
-
????public?class?WebService2?:?System.Web.Services.WebService
- ????{
- ????????[WebMethod]
-
????????public?int?GetRandom()
- ????????{
-
????????????return?new?Random(DateTime.Now.Millisecond).Next();
- ????????}
- ????????[WebMethod]
-
????????
-
????????
-
????????
-
????????[System.Web.Script.Services.ScriptMethod(UseHttpGet=true)]
-
????????public?int?GetRangeRandom(int?minValue,?int?maxValue)
- ????????{
-
????????????return?new?Random(DateTime.Now.Millisecond).Next(minValue,?maxValue);
- ????????}
- ????}
- }
页面代码: ?
-
<%@?Page?Language="C#"?AutoEventWireup="true"?CodeBehind="WebForm1.aspx.cs"?Inherits="AJAXEnabledWebApplication4.WebForm1"?%>
-
<!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">
-
????<asp:ScriptManager?runat="server"?ID="scriptManager1"?>
-
????<Services>
-
????<asp:ServiceReference?Path="WebService2.asmx"?InlineScript="true"?/>
-
????</Services>
-
????</asp:ScriptManager>
-
????<input?type="button"?value="GetRandom?(Post)"?onclick="getRandom()"?/>
-
????<input?type="button"?value="Get?Range?Random?(Get)"?onclick="getRangeRandom(50,100)"?/>
-
????<script?type="text/javascript"?language="javascript"?>
- ????function?getRandom(){
- ??????????AJAXEnabledWebApplication4.WebService2.GetRandom(onsucceed);
- ????}
- ????function?getRangeRandom(minValue,maxValue){
- ????????AJAXEnabledWebApplication4.WebService2.GetRangeRandom(minValue,maxValue,onsucceed);
- ????}
- ????function?onsucceed(result){
- ????????alert(result);
- ????}
-
????</script>
-
????</form>
-
</body>
-
</html>
在页面生成的代理中,可以看到一些区别,还可以通过httpWatch观察post和get 的一些小的区别.?
2,服务器方法重载的调用和客户端重载的调用
简单例子:
webservice代码: ?
-
using?System;
-
using?System.Collections;
-
using?System.ComponentModel;
-
using?System.Data;
-
using?System.Web;
-
using?System.Web.Services;
-
using?System.Web.Services.Protocols;
-
namespace?AJAXEnabledWebApplication4
- {
-
????
-
????
-
????
-
????[WebService(Namespace?=?"http://tempuri.org/")]
- ????[WebServiceBinding(ConformsTo?=?WsiProfiles.BasicProfile1_1)]
-
????[ToolboxItem(false)]
- ????[System.Web.Script.Services.ScriptService]
-
????public?class?WebService1?:?System.Web.Services.WebService
- ????{
- ????????[WebMethod]
-
????????public?int?GetRandom()
- ????????{
-
????????????return?new?Random(DateTime.Now.Millisecond).Next();
- ????????}
-
????????[WebMethod(MessageName="GetRangeRandom")]
-
????????
-
????????public?int?GetRandom(int?min,?int?max)
- ????????{
-
????????????return?new?Random(DateTime.Now.Millisecond).Next(min,?max);
- ????????}
- ????}
- }
aspx页面代码:
-
<%@?Page?Language="C#"?AutoEventWireup="true"?CodeBehind="Default.aspx.cs"?Inherits="AJAXEnabledWebApplication4._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>Untitled?Page</title>
-
</head>
-
<body>
-
????<form?id="form1"?runat="server">
-
????????<asp:ScriptManager?ID="ScriptManager1"?runat="server">
-
????????<Services>
-
????????<asp:ServiceReference?Path="WebService1.asmx"?InlineScript="true"?/>
-
????????</Services>
-
????????</asp:ScriptManager>
-
????????<input?type="button"?value="Get?Random"?onclick="GetRandom()"?/>
-
????????<input?type="button"?value="Get?Range?Random"?onclick="GetRandom(50,100)"?/>
-
????????<script?type="text/javascript"?language="javascript">
- ????????function?GetRandom(minValue,maxValue){
- ??????????//js?函数默认的?arguments可以判断传入参数的数量,从而模仿了客户端方法重载
- ???????????if(arguments.length!=2){
- ????????????AJAXEnabledWebApplication4.WebService1.GetRandom(onsucceed);
- ????????????}else{
- ????????????AJAXEnabledWebApplication4.WebService1.GetRangeRandom(minValue,onsucceed);
- ???????????
- ????????????}
- ????????}
- ????????
- ????????function?onsucceed(result){
- ????????????alert(result);
- ????????}
-
????????</script>
-
????</form>
-
</body>
-
</html>
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|