zoukankan      html  css  js  c++  java
  • 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

    生命中最值得欣慰的,莫过于一觉醒来,你还在身旁
  • 相关阅读:
    计算机网络【10】—— Cookie与Session
    计算机网络【9】—— HTTP1.0和HTTP1.1的区别及常见状态码
    计算机网络【8】—— Get和Post请求的区别
    计算机网络【7】—— TCP的精髓
    数据库事务的四大特性以及事务的隔离级别
    JavaWeb基础【1】—— Tomcat
    Get current time and date on Android
    Converting Storyboard from iPhone to iPad
    iPhone OS 开发
    (译)iOS Code Signing: 解惑
  • 原文地址:https://www.cnblogs.com/chaonuanxi/p/10409717.html
Copyright © 2011-2022 走看看