1,使用Htttp Get 访问方法
页面代码:
在页面生成的代理中,可以看到一些区别,还可以通过httpWatch观察post和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
- {
- /// <summary>
- /// WebService2 的摘要说明
- /// </summary>
- [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]
- //ScriptMethod属性标记
- //设置UserHttpGet为true
- //通过上面的设置,就可以设置请求方式为Get
- [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>
简单例子:
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
- {
- /// <summary>
- /// WebService1 的摘要说明
- /// </summary>
- [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")]
- //可以通过 MessageName=""对方法进行重新命名
- public int GetRandom(int min, int max)
- {
- return new Random(DateTime.Now.Millisecond).Next(min, max);
- }
- }
- }
- <%@ 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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。