C# XMLHttpRequest對象—Ajax實例


Get:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script type="text/javascript">
        window.onload = function () {
            document.getElementById("btnGet").onclick = function () {
                //第一步,創建XMLHttpRequest對象
                var xhr = null;
                if (typeof (XMLHttpRequest) != undefined) {
                    xhr = new XMLHttpRequest();
                }
                else {
                    xhr = new ActiveXObject("Microsoft.XMLHttp");
                }
                //第二部,設置異步回調函數。
                xhr.onreadystatechange = function () {
                    //xhr對象狀態,0=已創建XMLHttpRequest對象,1=open,2=send,3=onready等待響應,4=成功。
                    if (xhr.readyState == 4) {
                        //status 響應狀態
                        if (xhr.status >= 200 && xhr.status < 300 || xhr.status == 304) {
                            document.getElementById("div1").innerHTML = xhr.responseText; //xhr.responseText 響應體
                        }
                    }
                }
                //第三步,打開對象,設置請求方式和訪問路徑
                xhr.open("Get", "GetTime.ashx?id=17&name=" + window.encodeURIComponent("張三"), true);
                //第四步,send()
                xhr.send(null);
            };
        };
    </script>
</head>
<body>
    <div id="div1"></div>
    <input type="button" value="無刷新異步獲取" id="btnGet" />
</body>
</html>

 

Post:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script type="text/javascript">
        window.onload = function () {
            document.getElementById("btn").onclick = function () {
                var xhr = null;
                if (typeof (XMLHttpRequest) != undefined) {
                    xhr = new XMLHttpRequest();
                }
                else {
                    xhr = new ActiveXObject("Microsoft.XMLHttp");
                }
                xhr.onreadystatechange = function () {
                    if (xhr.readyState == 4 && xhr.status == 200) {
                        document.getElementById("div1").innerHTML = xhr.responseText + "<hr />";
                        //獲取響應報文頭信息
                        var date = xhr.getResponseHeader("Date");
                        document.getElementById("div2").innerHTML = date + "<hr />";
                        //獲取所有響應報文頭信息
                        var all = xhr.getAllResponseHeaders();
                        document.getElementById("div3").innerHTML = all + "<hr />";
                    }
                }
                //通過Post方式請求
                xhr.open("Post", "GetTime.ashx", true);
                //需要添加請求報文頭,Content-Type.
                xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                //通過鍵值對的方式傳值。
                var name = document.getElementById("name").value;
                xhr.send("id=18&name=" + name);
            };
        };
    </script>
</head>
<body>
    <div id="div1"></div>
    <div id="div2"></div>
    <div id="div3"></div>
    <input type="text" id="name" value="李四" />
    <input type="button" value="提交" id="btn" />
</body>
</html>

GetTime.ashx:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;

namespace MyPerson.UI.Ajax
{
    /// <summary>
    /// GetTime 的摘要說明
    /// </summary>
    public class GetTime : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //Thread.Sleep(1000);
            string id = context.Request["id"];
            string name = context.Request["name"];
            context.Response.Write(DateTime.Now.ToString() + "<br/>編號:" + id + "<br/>姓名:" + name);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 JQuery版:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="../Scripts/jquery-1.7.1.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#btnGet").click(function () {
                $.ajax({
                    type: "GET",
                    url: "GetTime.ashx",
                    data: {id: 17, name: "張三"},
                    dataType: "text",
                    success: function(data) {
                        $('#div1').html(data);
                    }
                });
            });
        });
    </script>
</head>
<body>
    <div id="div1"></div>
    <input type="button" value="無刷新異步獲取" id="btnGet" />
</body>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM