AJAX通過HTML請求C#一般處理程序


AJAX通過HTML請求C#一般處理程序

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <input type="button" value="Ajax提交" onclick="Ajax()" />
    <div id="resMsg"></div>
    <script>
        ////1.定義一個函數,通過該函數來異步獲取信息
        //function Ajax() {
        //    //2.聲明一個空對象來用裝XMLHTTPRequest對象
        //    var xmlHttpReq = null;
        //    //3.給XMLHTTPRequest對象賦值
        //    if (window.ActiveXObject)  //IE5 IE6 是已ActiveXObject的方式引入XMLHTTPRequest對象
        //    {
        //        xmlHttpReq = new ActiveXObjece("Microsoft.XMLHTTP");
        //    }
        //    else if (window.XMLHttpRequest) //出IE5 IE6以外的瀏覽器,XMLHttpRequest是window的子對象
        //    {
        //        xmlHttpReq = new XMLHttpRequest(); //實例化一個XMLHttpRequest
        //    }
        //    //4.實例化成功,使用Open方法初始化XMLHttpRequest對象,指定HTTP方法和要使用服務器的url
        //    //xmlHttpReq.open("GET", "/ashx/NewOneAjax.ashx?id=17&name=" + window.encodeURIComponent("張三", true));
        //    xmlHttpReq.open("GET", "/ashx/NewOneAjax.ashx",true);
        //        //調用open方法並采用異步方式
        //    //默認情況下,使用XMLHttpRequest對象發送的HTTP請求是異步進行的,但是可以顯示地吧async參數設置為true
        //    //5.因為要做一個異步調用
        //    xmlHttpReq.onreadystatechange = requestCallBack; //設置回調函數
        //    //6.使用Send方法發送該請求
        //    xmlHttpReq.send(null); //因為是Get提交,所以可以使用null作為參數調用
        //}

        //function requestCallBack()  //一旦readyState值改變,將會調用該函數
        //{
        //    if (xmlHttpReq.readyState == 4) {
        //        //status 響應狀態
        //        if (xmlHttpReq.status == 200) {
        //            //將xmlHttpReq.responseText的值賦予id為resMsg的元素
        //            document.getElementById("resMsg").innerHTML = xmlHttpReq.responseText;
        //        }
        //    }
        //}
            function Ajax() {
                //第一步,創建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) {
                            document.getElementById("resMsg").innerHTML = xhr.responseText; //xhr.responseText 響應體
                        }
                    }
                }
                //第三步,打開對象,設置請求方式和訪問路徑
                xhr.open("Get", "/ashx/NewOneAjax.ashx", true);
                //第四步,send()
                xhr.send(null);
            };
    </script>

</body>
</html>

  請求后端

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

namespace WebApplication1.ashx
{
    /// <summary>
    /// NewOneAjax 的摘要說明
    /// </summary>
    public class NewOneAjax : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World");
            //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;
            }
        }
    }
}

  轉載:https://www.cnblogs.com/han1982/p/4049642.html


免責聲明!

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



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