• WebService和Ajax


    阅读全文:http://www.cckan.net/forum.php?mod=viewthread&tid=60

    第一种把WebService写在页面之外的

    看代码吧,我写的有注释大家应该一看就明白了

    这是Ajax代码用来调用WebService里

    代码
    <form id="form1" runat="server">
        
    <input type="button" id="button" value="Random" onclick="GetRandom()" />
        
    <input type="button" id="button1" value="Random" onclick="GetRandom(50,100)" />
        
    <asp:ScriptManager ID="ScriptManager1" runat="server">
            
    <Services>
                
    <%--在这里引入WebService--%>
                
    <%-- Path是路经, InlineScript ="true"打印出所有的方法,一般为False默认的 --%>
                
    <asp:ServiceReference  Path="~/WebService.asmx" />
            
    </Services>
        
    </asp:ScriptManager>
        
    <script language="javascript" type="text/javascript">
           
    //调用WebService里的方法
          function GetRandom(minValue,maxValue)
          {
                
    //在Script里不能直接,也不可能重载方法,用这种形式来重载是解决这个问题的一种方法,根据参数返回不同的值
               if(arguments .length!=2)
               {
                   WebService.getRandom(getrandom);
               }
    else 
               {
                   WebService.getRandomisMaxValueorMinValue(minValue ,maxValue,getrandom);
               }
          }
          
    //回调这个函数用于返回一个值
          function getrandom(result)
          {
               alert (result);
          }
        
    </script>

        
    </form>

    webService里的代码

    代码
    using System;
    using System.Collections;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    using System.Xml.Linq;
    using System.Web.Script.Services;

    /// <summary>
    ///WebService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo 
    = WsiProfiles.BasicProfile1_1)]
    //若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 
    [ScriptService]
    public class WebService : System.Web.Services.WebService
    {
        
    public WebService()
        {
            
    //如果使用设计的组件,请取消注释以下行
            
    //InitializeComponent(); 
        }

        
    /// <summary>
        
    /// 产生一个随机数
        
    /// </summary>
        
    /// <returns>INT型</returns>
        [WebMethod]
        
    public int getRandom()
        {
            
    return new Random(DateTime.Now.Millisecond).Next();
        }

        
    /// <summary>
        
    /// 产生一在一定范围内的随机数
        
    /// </summary>
        
    /// <param name="MinValue">参数范围的最小值</param>
        
    /// <param name="MaxValue">参数范围的最大值</param>
        
    /// <returns>INT型</returns>
        [WebMethod]
        
    public int getRandomisMaxValueorMinValue(int MinValue, int MaxValue)
        {
            
    return new Random(DateTime.Now.Millisecond).Next(MinValue, MaxValue);
        }
    }

     第二种是在界面下面写的

    看代码吧

    WebService代码


       

     //一个服务器端的WerService
        [WebMethod]
        
    public static DateTime getDateTime()
        {
            
    //显示的是带时区的时间
            return DateTime.UtcNow;
        }

    界面里调用的代码

    代码
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageMethods.aspx.cs" Inherits="WebService_PageMethods" %>

    <!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 ID="ScriptManager1" runat="server" EnablePageMethods="true">
        
    </asp:ScriptManager>
        
    <input type="button" value="time" onclick="Dateime()" />
        
    <%-- //这样做是不行的,会找不到方法--%>
        
    <%-- <asp:Button ID="Button1" runat="server" Text="Button"  OnClick ="Dateime()"/>--%>

        
    <script language="javascript" type="text/javascript">
        
    function Dateime()
        {
            PageMethods.getDateTime(getDatetime);
        }
        
    function getDatetime(result)
        {
           
    return alert(result);
        }
        
    </script>

        
    </form>
    </body>
    </html>


     

    本人的博客不再维护从2013年就不再维护了 需要我帮助的朋友请到我的个人论坛 http://www.sufeinet.com 进行讨论,感谢大家对我的支持!
  • 相关阅读:
    poj 1466 Girls and Boys
    poj 1486 Sorting Slides
    poj 2112 Optimal Milking
    poj 1274 The Perfect Stall
    SHoj 420 购买装备
    poj 2987 Firing
    SHoj A序列
    FOJ Problem 2271 X
    XidianOJ 1028 数字工程
    XidianOJ 1030 三数和
  • 原文地址:https://www.cnblogs.com/sufei/p/1430952.html
Copyright © 2020-2023  润新知