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文件

  • 相关阅读:
    shell脚本中生成延时
    linux小技巧
    自定义微信圈分享带的图片和内容
    OOM killer
    svn报错
    Fatal error: Call-time pass-by-reference has been removed
    ThinkPHP3.1.3源码分析---php文件压缩zlib.output_compression 和 ob_gzhandler
    确保 PHP 应用程序的安全
    判断来自电脑还是手机
    以About Us为范例在Zen cart中增加页面
  • 原文地址:https://www.cnblogs.com/clc2008/p/2095140.html
Copyright © 2011-2022 走看看