嘿嘿。今天學習了AJAX的幾個方法

來源:互聯網
上載者:User

標籤:

                   今天學習了AJAX的幾個方法,其實我很早在公司實習的時間就認識了它,但是對它一無所知,也並沒有去學習它,今天學習它讓我感到很興奮因為重新瞭解了它,嘿嘿,下面就來總結一下今天學習的吧。

          一.在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: "[email protected]" }, 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:成功後的回呼函數。
                       今天就簡單的總結到這裡啦,已經很晚啦,嘿嘿,休息。加油加油!!!

嘿嘿。今天學習了AJAX的幾個方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.