最简单的Ajax


        //******************AJAX*************************
        function createXmlHttp() {//创建xhr对象
            var xhobj = false;
            try {
                xhobj = new ActiveXObject("Msxml2.XMLHTTP"); // ie msxml3.0+
            } catch (e) {
                try {
                    xhobj = new ActiveXObject("Microsoft.XMLHTTP"); //ie msxml2.6
                } catch (e2) {
                    xhobj = false;
                }
            }
            if (!xhobj && typeof XMLHttpRequest != 'undefined') {// Firefox, Opera 8.0+, Safari
                xhobj = new XMLHttpRequest();
            }
            return xhobj;
        }
       
        //声明变量
        var xhr = false;
        var txt ;
        //在浏览器加载完所有的页面资源后创建 异步对象
        window.onload = function () {
            xhr = createXmlHttp(); //创建 异步对象
            //js 取服务端控件
          // txt = document.getElementById("<%=TextBYzm.ClientID %>")
         //   txt.onblur = function () { alert("111") };
            txt.onblur = doAjax;

        }
        //点击按钮时调用此方法,使用AJAX到服务器拿数据
        function doAjax() {
            //设置参数
           // alert("000");
            var url = "VerCode.ashx?isAjax=1&txt=" + document.getElementById("<%=TextBYzm.ClientID %>").value;
            xhr.open("GET", url, true);
            //设置回调函数  watching (不是 watching())
            xhr.onreadystatechange = watching;
            //出发
            xhr.send(null);
          
         
        }
        //回调函数
        function watching() {
            //检查 异步对象 的准备状态是否=4,如果等于4说明服务器已经将数据发回给异步对象了
            if (xhr.readyState >= 4) {
                if (xhr.status == 200) {//正常返回,判断服务器返回的状态码是否=200
                    var session = xhr.responseText; //获得服务器返回的字符数据
                 //  alert(session);
                   // if (session != txt.value) {
                       // document.getElementById("<%=Label1.ClientID %>").innerText = "验证码错误";
                        //禁止表单提交
                       // return false;
                    }
                    else {
                        document.getElementById("<%=Label1.ClientID %>").innerText = "";
                    }
                 
                } else {
                    //                    document.getElementById("YZM").title = "服务器出错啦!" + xhr.status;
                    alert(xhr.status);
                }
            }
        }

************************后台asp.net*****************************

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

namespace WebApplicationV3
{
    /// <summary>
    /// CheckVercode 的摘要说明
    /// </summary>
    public class CheckVercode : IHttpHandler,System.Web.SessionState.IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            string txt = context.Request.QueryString["txt"].ToString();
           string session= context.Session["ValidCode"].ToString();
           if (txt==session)
           {
               context.Response.Write("1");
           }
           else
           {
               context.Response.Write("0");
           }
           // context.Response.ContentType = "text/plain";
           
        }

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

原文地址:https://www.cnblogs.com/520cc/p/2735902.html