几种Ajax方法

来源:互联网 发布:零起点学通c语言 pdf 编辑:程序博客网 时间:2024/04/20 03:20

Ajax方法1

前台aspxjs代码:

<scriptlanguage="javascript"type="text/javascript">

function CallServer()

{

var a = document.getElementById("text").value;

<%=ClientScript.GetCallbackEventReference(this,"a","ReceiveServer","null")%>//回调方法

 }

        //返回结果

function ReceiveServer(result)

{

       document.getElementById("text").value = result;

}

</script>

 

 

 

后台cs代码:

#region 回调(一定要用这两个方法  而且名字还是RaiseCallbackEvent跟GetCallbackResult)

private stringResult;

//用来获取前台返回的值 

public voidRaiseCallbackEvent(string eventArgument)

{

     Result = eventArgument;

 }

//用来返回给前台的值

public stringGetCallbackResult()

{

      Console.WriteLine("处理前Result" +Result);

      int i = int.Parse(Result);

      i++;

      Result = i.ToString();

      Console.WriteLine("处理后Result" +Result);

      return Result + " " + DateTime.Now.ToString();

 }

 #endregion

 

 

 

Ajax方法2

前台aspx代码:

<scriptsrc="jQuery.js" type="text/javascript"></script>

<script src="jquery-1.7.1.js"></script>

<script type="text/javascript">

function testGet() {

            $.ajax({

                type: post, //方式post get

                url: 'NormalPage.aspx', //指向的URL

                async: true,

                success: function (result) {

                    alert(result); //处理后台的返回值

                },

                error: function (){

                    alert('ERROR!');

                }

            });

        }

</script>

 

 

后台NormalPage.aspx的代码:

在PageLoad里面:

protected voidPage_Load(object sender, EventArgs e)

{

     string action = Request.QueryString["action"];

     Response.Clear(); //清除所有之前生成的Response内容

     Response.Write(action + " " +DateTime.Now.ToString());

     Response.End(); //停止Response后续写入动作,保证Response内只有我们写入内容

}

 

 

Ajax方法3

function ajax()

{

var xmlhttp = window.XMLHttpRequest ? newXMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); //创建XMLHTTP对象,考虑兼容性

xmlhttp.open("POST", "Handler1.ashx?"+ "i=" + document.getElementById("txt1").value,true); //“准备”向服务器的AJAXTest.ashx发出Post请求(GET可能会有缓存问题)。这里还没有发出请求

           xmlhttp.onreadystatechange = function ()

           {

                if (xmlhttp.readyState == 4)//readyState == 4 表示服务器返回完成数据了。之前可能会经历2(请求已发送,正在处理中)、3(响应中已有部分数据可用了,但是服务器还没有完成响应的生成)

                {

                    if (xmlhttp.status == 200)//如果状态码为200则是成功

                    {

                       document.getElementById("txt1").value= xmlhttp.responseText;

                    }

                    else

                    {

                        alert("AJAX服务器返回错误!");

                    }

                }

           }

//不要以为if (xmlhttp.readyState == 4) {在send之前执行!!!!

           xmlhttp.send(); //这时才开始发送请求

//发出请求后不等服务器返回数据,就继续向下执行,所以不会阻塞,界面就不卡了,这就是AJAX中“A”的含义“异步”。试着在ashx加一句//Thread.Sleep(3000);

}

//下面写两个input

//<inputtype="button" value ="发送"onclick="ajax()"></input>

//<input type="text" id="txt1" value ="1"/>

 

 

后台Handler1.ashx代码:

在ProcessRequest里面 如果是aspx文件,就写在Page_Load里面

public voidProcessRequest(HttpContext context)

{

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

      string Request_a = context.Request["i"];

      int Request_i = Convert.ToInt32(Request_a);

      Request_i++;

     context.Response.Write(Request_i.ToString());

}

 

 

Ajax方法4

前台test.aspx代码:

<scripttype="text/javascript">

        functionajax()

        {

            var a =document.getElementById("te1").value;

            var b =WebForm3.ChangeValue(a).value;

            document.getElementById("te1").value = b;

        }

</script>

 

//下面有两个input

//<inputtype="button" id ="bt1" value="发送"onclick="ajax()"/>

//<input type="text" id="te1" value ="1"/>

 

 

后台test.cs代码:

首先引用Ajax.dll

usingAjax;

 

在PageLoad里面

protected void Page_Load(object sender, EventArgs e)

    {                                          //命名空间        类名

         Ajax.Utility.RegisterTypeForAjax(typeof(WebApplication4.WebForm3));

}

 

[Ajax.AjaxMethod()]

    public stringChangeValue(string res)

    {

         int i =Convert.ToInt32(res);

         i++;

         returni.ToString();

}

 

 

在web.config里面

<system.webServer>里面

添加:

<handlers>

      <addname="ajax"verb="POST,GET"path="ajax/*.ashx"type="Ajax.PageHandlerFactory, Ajax" />

</handlers>

0 0