zoukankan      html  css  js  c++  java
  • 简单ajax+webservice 例子

    一直以为webservice只用来提供外部特殊商务服务的功能模块,所以我一直都是用ashx文件处理后台,今天看了别人的文章恍然明白,原来webservice也可以处理ajax请求,而且一个文件可以同时处理多个请求,和ashx方便多了,不用建立那么文件了。

    下面是2个很简单的例子 ,明白道理即可

    HTML部分

    View Code
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        
    <title>无标题页</title>
        
    <script type="text/javascript" src="js/jquery-1.6.1.min.js"></script>
        
    <script type="text/javascript">
        
    //第一个测试例子
        function HelloWorld()
        {
             $.ajax({
               type: 
    "POST",
               url: 
    "WebService.asmx/HelloWorld",
               data: 
    "name=John",
               success: 
    function(msg){
                 alert( 
    "Data Saved: " + msg );
               }
            }); 
        } 
        
    //两数字相加
        function add()
        {
               $.ajax({
               type: 
    "POST",
               url: 
    "WebService.asmx/Add",
               data: 
    "a="+$("#Text1").val()+"&b="+$("#Text2").val(),
               success: 
    function(msg){
                
                $(
    "#Text3").val(msg);
               }
            }); 
        
        }
     
        
    </script>
        
    </head>
    <body>
        
    <form id="form1" runat="server">
        
    <div>
            
    <input id="hello" type="button" value="button" onclick="HelloWorld()" />
           
            
    <br />
            
            
            
    <br />
            NumA:
    <input id="Text1" style=" 31px" type="text" />
            +NumB:
    <input id="Text2"style=" 33px" type="text" onblur="add()" />
            =
    <input id="Text3" style=" 33px" type="text" /></div>
        
    </form>
                
    </body>
    </html>

    webservice部分

    View Code
    using System;
    using System.Web;
    using System.Collections;
    using System.Web.Services;
    using System.Web.Services.Protocols;


    /// <summary>
    /// WebService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo 
    = WsiProfiles.BasicProfile1_1)]
    public class WebService : System.Web.Services.WebService {

        
    public WebService () {

            
    //如果使用设计的组件,请取消注释以下行 
            
    //InitializeComponent(); 
        }

        [WebMethod]
        
    public void HelloWorld() {       
            
    if (Context.Request["name"!= null)
            {
                Context.Response.Write(Context.Request[
    "name"].ToString());
            }
            
    else
            {
                Context.Response.Write(
    "no");
            }
        }
        [WebMethod]
        
    public string Hello(string name)
        {
            
    return string.Format("Hello {0}", name);
        }
        [WebMethod]
        
    public void Add()
        {
            
    if (Context.Request["a"!= null && Context.Request["a"!= null)
            {
             
                
    int anum = int.Parse(Context.Request["a"].ToString());
                
    int bnum = int.Parse(Context.Request["b"].ToString());
                
    int result=anum+bnum;
                Context.Response.Write(result.ToString());
            }
            
    else
            {
                Context.Response.Write(
    "no");
            }
          
        }

        
    }

    不要忘记引用jquery.js文件

  • 相关阅读:
    HDU Railroad (记忆化)
    HDU 1227 Fast Food
    HDU 3008 Warcraft
    asp vbscript 检测客户端浏览器和操作系统(也可以易于升级到ASP.NET)
    Csharp 讀取大文本文件數據到DataTable中,大批量插入到數據庫中
    csharp 在万年历中计算显示农历日子出错
    csharp create ICS file extension
    CSS DIV Shadow
    DataTable search keyword
    User select fontface/color/size/backgroundColor设置 字体,颜色,大小,背景色兼容主流浏览器
  • 原文地址:https://www.cnblogs.com/clc2008/p/2095140.html
Copyright © 2011-2022 走看看