ajax

来源:互联网 发布:上海迈博软件 编辑:程序博客网 时间:2024/06/18 06:54

              

          一.在javascript中写AJAX

复制代码
<script>        window.onload = function () {            document.getElementById("txtName").onblur = function () {                var xml = new XMLHttpRequest(); //1 首先要创建异步对象                xml.open("get", "JSAjax.ashx", true);//2 以get方式打开要发送的地址                xml.onreadystatechange = function () {                    if (xml.statusText == 4) {                        alert(xml.responseText);//当接受状态等于4的时候,已经接受到了服务器传递过来的值。                    }                }                xml.send(null);//发送邮件,当为get方式时间发送的请求为空            }        }        //window.onload = function () {        //    document.getElementById("txtName").onblur = function () {        //        var xml = new XMLHttpRequest();        //        xml.open("post", "JSAjax.ashx", true);        //        xml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");        //        xml.onreadystatechange = function () {        //            if (xml.statusText == 4) {        //                alert(xml.responseText);        //            }        //        }        //        xml.send("txtname="+this.value)        //    }        //}    </script></head><body>    <input type="text" name="txtname" id="txtName"/></body></html>
复制代码
复制代码
  public void ProcessRequest (HttpContext context) {        context.Response.ContentType = "text/plain";        string name = context.Request.QueryString["txtname"];        //string name = context.Request.Form["txtname"];        if (!string.IsNullOrEmpty(name))        {            context.Response.Write("您的用户名"+name + "可用");        }        else        {            context.Response.Write("您的用户名不可用");        }    }
复制代码

                      上面是在javascript中写的ajax,Ajax在本质上是一个浏览器端的技术,Ajax技术之主要目的在于局部交换客户端及服务器间之数据,这个技术的主角XMLHttpRequest的最主要特点,在于能够不用重新载入整个版面来更新资料,也就是所谓的Refresh without Reload(轻刷新)与服务器之间的沟通,完全是透过Javascript来实行,使用XMLHttpRequest本身传送的数据量很小,所以反应会更快,也就让网络程式更像一个桌面应用程序,AJAX 就是运用Javascript 在后台悄悄帮你去跟服务器要资料,最后再由Javascript 或DOM 来帮你呈现结果,因为所有动作都是由Javascript代劳,所以省去了网页重载的麻烦,使用者也感受不到等待的痛苦。使用XMLHttpRequest对象  按照下面模式,可以同步地XMLHttpRequest对象:创建对象;-new创建请求; -open()发送请求;-send(),但是使用javascript比较麻烦,于是就改变为了jquery的使用方法。
                   二.JQuery中写AJAX

          1.AJAX的$.Load事件( url,[,data][.callback])

复制代码
<script src="Scripts/jquery-1.7.1.min.js"></script>    <script>        $(function () {            $("#Send").click(function () {                $("#resText").load("Ajax.ashx", { txtemail: "123@qq.com" }, function (msg) {                    alert(msg);                });            });        });    </script><body>    <input type="button"  value="AjaxLoad "  id="Send"/>    <div class="comment">        已有评论    </div>    <div id="resText">       </div></body>
复制代码
复制代码
public void ProcessRequest (HttpContext context) {        context.Response.ContentType = "text/plain";        string email = context.Request.Form["txtemail"];        if (!string.IsNullOrEmpty(email))        {            context.Response.Write("<span>您的邮箱地址为"+email+"可用</span>");        }        else        {            context.Response.Write("<span>您的邮箱地址为" + email + "不可用</span>");        }    }
复制代码

                  url:发送的地址,data:发送给服务器的键值对,callback:回调函数。

          2.$.Get和$.Post方法

复制代码
 <script>         $(function () {             //$("#send").click(function () {             //    $.get("JQuery.ashx", { username: $("#username").val(), content: $("#content").val() }, function (msg) {             //        $("#resText").html(msg);             //    });             //});             $("#send").click(function () {                 $.post("JQuery.ashx", { username: $("#username").val(), content: $("#content").val() }, function (msg) {                     $("#resText").html(msg);                 });             });         });     </script><body>    <form id="form1" action="#">        <p>评论:</p>        <p>姓名:<input type="text" name="username" id="username"  /></p>        <p>内容:<textarea name="content" id="content" rows="2" cols="20"></textarea></p>        <p><input type="button" name="name" value="提交 " id="send" /></p>    </form>    <div class="comment">        已有评论    </div>    <div id="resText">    </div></body>
复制代码

 

复制代码
public void ProcessRequest (HttpContext context) {        context.Response.ContentType = "text/plain";        //string name = context.Request.QueryString["username"];        //string content = context.Request.QueryString["content"];        string name = context.Request.Form["username"];        string content = context.Request.Form["content"];        if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(content))        {            context.Response.Write("<span>"+name+"评论:"+content+"</span>");        }    }
复制代码

          url:发送的地址,data:发送给服务器的键值对,callback:回调函数。

                  3.$.ajax方法

复制代码
<script>        $(function () {            $("#send").click(function () {                $.ajax({                    type: "post",                    url: "1.js",                    dataType: "script",                    success: function (msg)                    {                        alert(msg);                    }                });            });        })    </script>
复制代码

                       url:发送的地址,type:请求的类型,timeout:请求时间,beforesend是在请求之前,complete:回调函数,success:成功后的回调函数。
                     


0 0
原创粉丝点击