AJAX

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LogIn.aspx.cs" Inherits="LogIn" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="Script/jquery-1.7.1.min.js"></script>
    <script type="text/javascript">
        function myXMLHttpRequest()
        {
            //创建AJAX对象,不同浏览器创建方法不同,在这简写了
            var xmlhttp;
            if (window.XMLHttpRequest) {
                xmlhttp = new XMLHttpRequest();
                
            }
            else {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");               
            }

            //AJAX对象状态发生改变时触发
            xmlhttp.onreadystatechange = function ()
            {
                
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
                {
                    //responseText 响应返回类型为Text时,用这个属性接收
                    //修改相应标签的属性
                    document.getElementById("sp").innerHTML = xmlhttp.responseText;                   
                    
                }
            }
            var url = "/AJAX练习/xiangying.aspx?username=" + $("#txt").val();
            //发出请求
            //第一个参数GET/POST方式
            //第二个参数请求页面的地址
            //第三个参数是否使用异步机制,true
            xmlhttp.open("GET", url, true);            
            xmlhttp.send();

        }
        
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr>
            <td>
                用户名:
            </td>
            <td>
                <input id="txt" type="text" onkeyup="myXMLHttpRequest()" />
            </td>
            <td>
                </td>
            <td>
                <span id="sp"></span>
            </td>
        </tr>
        <tr>
            <td>
                密码:
            </td>
            <td>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </td>
        </tr>
    </table>
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class xiangying : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //接收请求
        string userna = Request["username"].ToString();
        //响应请求
        if (userna=="abc")
        {
            Response.Write("可以");
        }
        else
        {
            Response.Write("不可以");
        }
    }
}

以下是用JQuery写的 返回类型是XML格式

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="Js/jquery-1.11.2.js" type="text/javascript" ></script>

    <script>

        $(document).ready(
            function () {
                $("#txtUid").blur(
                    function () {
                        var txt = $(this).val();
                        //ajax发送文本信息出去
                        $.ajax({
                            url:"Default2.aspx",//接收请求的页面
                            type: "POST",//请求发送方式
                            data: { uid: txt },//数据
                            datatype: "XML",//接收回送的信息格式设定
                            success: function (data) {//回调函数
                                var c = $(data).text();
                                    $("#Literal1").html("用户名可用");
                            }
                            });
                    }
                    );
            }
            );

    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:TextBox ID="txtUid" runat="server"></asp:TextBox>
        <div id="Literal1"></div>
        
        <br />
    
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string uid = Request["uid"].ToString();

        Response.Write("<?xml version='1.0'?>");
        Response.Write("<count id='count'>OK</count>");
        Response.End();
        
    }
}

 

posted @ 2015-08-03 09:46  九省巡按  阅读(183)  评论(0编辑  收藏  举报