jquery ajax与asp.net

 

 

通常我们用3种方法实现jquery  ajax方法实现页面无刷新返回数据的异步交互功能

本人花费了很大的功夫才一一解析,网页上虽有很多案例,但是某些人可能做到了,但没有给

详细的说明

 

1.       使用aspx页面

//特别注意了,这里的aspx页面只是作为一个方法的载体,一定要删除了<html></html>中的全部内容(包括<html>本身也要删除)

//这时候 page.aspx页面只剩下

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="page.aspx.cs" Inherits="Jquery.WebService" %>

 

我们要用的是page.aspx.cs里面的

protected void Page_Load(object sender, EventArgs e)

 {

//当请求发送时,页面的Page_Load开始被执行

//首先 Request.From[“keyname”]方法实现 提交的数据 

// 其次    Request.From[“keyname”]的数据进行处理

// string  data =”I have been processed!”;

//最后 Response.Write(data);返回数据

}

//由此可见,我们为了请求一个方法而去单独创建了一个页面,是有点浪费了!

 

下面是一个案例

 

use_aspx.aspx里的内容

 

$().ready(function () {

             $('#user-ajax').click(function () {

                $.ajax({

                    asnc: true,

                    type: "POST",

                    dataType: 'text',

                    url: "page.aspx",//这里请求page.aspx页面                

                    data: "id=kk&name=fff",

                   

                    success: function (result) { alert(result); },

                    error: function (result, status) { alert(status + " sorry!"); }

 

                });

            })

        });

          

 

 

page.aspx.cs里的  Page_Load(object sender, EventArgs e)方法

 

protected void Page_Load(object sender, EventArgs e)

        {

            string id = Request.Form["id"].ToString();

            string name = Request.Form["name"].ToString();

            Response.Write("id:"+id+"name:"+name);// string的类型

        }

 

2.       使用ashx页面

//ashx里我们可以选择方法

  //下面是一个案例

 

//use_ashx.aspx页面内容

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

    <script type="text/javascript">

        $().ready(function () {

            $('#user-ajax').click(function () {   

                $.get("Handler1.ashx", { method: "method1", txt1: $("#Text1").text() }, function (msg) { $("#Text1").text(msg); } );

    

            });

        });

    </script>

</head>

<body>

    

     

      <input id="user-ajax" type="button" value="click" />

       <div id="Text1" style="height:100px;border:1px solid Gray; color:Blue;">initial data</div>

   

</body>

</html>

 

//Handler1.ashx.cs中的内容

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

 

namespace Jquery

{

    /// <summary>

    /// Summary description for Handler1

    /// </summary>

    public class Handler1 : IHttpHandler

    {

 

        public void ProcessRequest(HttpContext context)

        {

            context.Response.ContentType = "text/plain";

            String txt = context.Request["txt1"].ToString();

            String method = context.Request["method"].ToString();

            switch (method)

            {

                case  "method1":

                   txt= Method1(txt);

                    break;

                case "method2":

                    txt = Method1(txt);

                    break;

                case "method3":

                    txt = Method1(txt);

                    break;

             

            }

            context.Response.Write(txt + "Hello World");

        }

        public string Method1(string para)

        {

            return para+" is dealed by method 1";

        }

 

        public string Method2(string para)

        {

            return para + " is dealed by method 3";

        }

        public string Method3(string para)

        {

            return para + " is dealed by method 3";

        }

        public bool IsReusable

        {

            get

            {

                return false;

            }

        }

    }

}

 

3.      使用asmx页面

//asmx里我们可以选择方法

  //下面是一个案例

 

//use_asmx.aspx

<head runat="server">

    <title></title>

    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

    <script type="text/javascript">

        

        $().ready(function () {

         

            $('#user-ajax').click(function () {

                $.ajax({

                    asnc: true,

                    type: "POST",

                    dataType: 'xml',//这里是为了使用jquery的便利,获得丰富的数据,才使用xml

                    url: "WebService1.asmx/HelloWorld",//选择了HelloWorld方法

                    data: "id=kk&name=fff",//最好使用这种传统形式

                   // contentType: 'application/html; charset=utf-8',//这一行不要写

 

                    success: function (result)                          {   alert($(result).find("string").first().text()); },  

                    error: function (result, status) { alert(status + " sorry!"); }

 

                });

            })

        });

          

       

    </script>

</head>

 

 

//WebService1.asmx.cs里面的代码

//WebService1.asmx.cs利于2个方法,我们选择性的请求了方法HelloWorld()

 

        [WebMethod]

        public List<string> HelloWorld(string id,string name )

        {

             List<string> strList =new List<string>();

             strList.Add("aaaaaaaa");

             strList.Add("bbbbbbbbb");

             strList.Add("cccccccccccc");

             strList.Add("vvvvvvvvvvvv");

            return strList;

        }

 

 

        [WebMethod (EnableSession=true)]

        public static string GetWish(string value1 , string value2, string value3)

        {

            return String.Format("Á¡ê¨²¨²{3}¨º¤? {0}¡é{1}¡é{2}", value1, value2, value3);

        }

 

//三种异步请求方式建议使用asmx,也可以自己权衡

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值