简单ajax+webservice 例子

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

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

HTML部分

ExpandedBlockStart.gif 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 ="width: 31px"  type ="text"   />
        +NumB:
< input  id ="Text2" style ="width: 33px"  type ="text"  onblur ="add()"   />
        =
< input  id ="Text3"  style ="width: 33px"  type ="text"   /></ div >
    
</ form >
            
</ body >
</ html >

webservice部分

 

ExpandedBlockStart.gif 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文件

 

转载于:https://www.cnblogs.com/clc2008/archive/2011/07/01/2095140.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值