JQuery在asp.net中三种ajax传值

1)通过webservice,注意去掉注释[System.Web.Script.Services.ScriptService]这行前的注释

2)通过aspx.cs文件中的静态方法

3)通过aspx文件url

 

ExpandedBlockStart.gif WebForm1.aspx
<% @ Page Language = " C# "  AutoEventWireup = " true "  CodeBehind = " WebForm1.aspx.cs "  Inherits = " asp.net.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 >
    
< script src = " Scripts/jquery-1.4.1.js "  type = " text/javascript " ></ script >
    
< script type = " text/javascript " >
        function Ws() {
            $.ajax({
                type: 
" POST " ,
                contentType: 
" application/json; charset=utf-8 " ,
                url: 
" WebService1.asmx/HelloWorld2 " ,
                data: 
" {name:'xiaoxiao'} " ,
                dataType: 
' json ' ,
                success: function (result) {
                    alert(result.d);
                }
            });
        }
        function StaticMethod() {
            $.ajax({
                type: 
" POST " ,
                contentType: 
" application/json; charset=utf-8 " ,
                url: 
" aspxpage.aspx/SayHello2 " ,
                data: 
" {name:'xiaoxiao'} " ,
                dataType: 
' json ' ,
                success: function (result) {
                    alert(result.d);
                }
            });

        }
        function FromPage() {
            $.ajax({
                type: 
" POST " ,
                contentType: 
" application/json; charset=utf-8 " ,
                url: 
" dataContent.aspx?nowtime=' "   +   new  Date()  +   " ' " ,
                data: 
" {} " ,
                dataType: 
' html ' ,
                success: function (result) {
                    alert(result);
                }
            });

        }

    
</ script >
</ head >
< body >
    
< form id = " form1 "  runat = " server " >
     
    
< div >
        
< input id = " Button1 "  type = " button "  value = " jquery调用WebService "  onclick = " Ws() "   />
    
</ div >
    
< div >
        
< input id = " Button2 "  type = " button "  value = " jquery调用aspx页面静态方法 "  onclick = " StaticMethod() "   />
    
</ div >
    
< div >
        
< input id = " Button3 "  type = " button "  value = " jquery通过page存储值 "  onclick = " FromPage() "   />
    
</ div >
    
</ form >
</ body >
</ html >

以上是启动页面,WebForm1.aspx.cs没有代码

---------------------------------------

 

 

ExpandedBlockStart.gif WebService1.asmx
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Web;
using  System.Web.Services;

namespace  asp.net
{
    
///   <summary>
    
///  WebService1 的摘要说明
    
///   </summary>
    [WebService(Namespace  =   " http://tempuri.org/ " )]
    [WebServiceBinding(ConformsTo 
=  WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(
false )]
    
//  若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
     [System.Web.Script.Services.ScriptService]
    
public   class  WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        
public   string  HelloWorld()
        {
            
return   " Hello World " + System.DateTime.Now.ToLongTimeString();
        }

        [WebMethod]
        
public   string  HelloWorld2( string  name)
        {
            
return   " Hello World "   +  name  +  System.DateTime.Now.ToLongTimeString();
        }
    }
}
 

以上是webservice中的代码

------------------------------

 

ExpandedBlockStart.gif aspxpage.aspx.cs
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.Services;

namespace  asp.net
{
    
public   partial   class  aspx页面代替ws : System.Web.UI.Page
    {
        
protected   void  Page_Load( object  sender, EventArgs e)
        {
           
        }
        [WebMethod] 
        
public   static   string  SayHello()
        {
            
return   " Hello " ;
        }

        [WebMethod]
        
public   static   string  SayHello2( string  name)
        {
            
return   " Hello " + name;
        }
    }
}

以上是针对第二条 通过aspx.cs中的静态方法 注意方法前要加 [WebMethod],aspxpage.aspx页面没代码.

-----------------------------------

 

ExpandedBlockStart.gif dataContent.aspx.cs
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.IO;

namespace  asp.net
{
    
public   partial   class  dataContent : System.Web.UI.Page
    {
        
protected   void  Page_Load( object  sender, EventArgs e)
        {
            Response.Clear();
            Page.ViewStateMode 
=  ViewStateMode.Disabled;
            
if  (Request.QueryString[ " nowtime " !=   null )
            {
                
string  stime  =  Request.QueryString[ " nowtime " ].ToString();
                Response.Write(stime);
            }
            Response.Flush();
          
        }
    }
}

以上是针对第三条 用url传值 通过aspx页面保存数据。dataContent.aspx页面没有代码.

-------------------------

转载于:https://www.cnblogs.com/master-zhu/archive/2010/06/18/1760327.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值