ajax 判断账户密码 调取数据模糊查询 时钟

一.判断账户密码

《Login.html》

 1 <head>
 2 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 3     <title></title>
 4     <script src="Script/jquery.js"></script>
 5     <script language="javascript">
 6         $(document).ready(function () {
 7             $("#Submit1").click(function () {
 8                 //点击  获取 txtUID    txtPWD  的值  
 9                 var u = $("#txtUID").val();
10                 var p = $("#txtPWD").val();
11 
12                 //send request
13                 $.ajax({
14                     url: "Login.aspx",
15                     //将获取的值  传入login.asox 
16                     //data(name,值)
17                     data:{uid:u,pwd:p},
18                     type:"POST",
19                     dataType:"Text",
20                     success: function (data) {
21                         //接受传过来的值 判断数据 是否存在
22                         if (data == "1") {  //登录成功
23                             $("#d1").css("display", "none");
24                             $("#d2").css("display", "block");
25                             $("#ss").text(u);
26                         }
27                         else {  //登录失败
28                             alert("错了");
29                         }
30                     }
31                 });
32 
33                 return false;
34             });
35 
36         });
37 
38     </script>
39     <meta charset="utf-8" />
40 </head>
41 <body>
42     
43         <form id="f1" action="Login.aspx" method="get">
44             <div id="d1">
45                 账号:<input name="txtUID" id="txtUID" type="text" /><br />
46                 密码:<input name="txtPWD" id="txtPWD" type="text" /><br />
47                 <input id="Submit1" type="submit" value="提交" />
48             </div>
49             <div id="d2" style="display:none;">
50                 欢迎您,<span id="ss"></span>
51             </div>
52         </form>
53     </body>

 《Lojin.aspx》后台代码

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

public partial class Login : System.Web.UI.Page
{
    string[] u = { "aaa","bbb","ccc"};
    string[] p = { "aaa", "bbb", "ccc" };
    protected void Page_Load(object sender, EventArgs e)
    {
        string uid = Request["uid"].ToString();
        string pwd = Request["pwd"].ToString();
        //string uid = Request.QueryString["txtUID"].ToString();
        //string pwd = Request.QueryString["txtPWD"].ToString();
        //string uid = Request.Form["txtUID"].ToString();
        //string pwd = Request.Form["txtPWD"].ToString();
        //Response.Write(uid+"<br>"+pwd);
        //contains  包含关系 
        if (u.Contains(uid) == true && p.Contains(pwd))
        {
            Response.Write("1");
        }
        else
        {
            Response.Write("0");
        }
        Response.End();
    }
}

 二.调取数据模糊查询

 《Shownation.html》

 1 <head>
 2 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 3     <title></title>
 4     <script src="Script/jquery.js"></script>
 5     <script language="javascript">
 6         $(document).ready(function () {
 7             $("#b").click(function () {
 8                 //取文本框的值
 9                 var s = $("#n").val();
10                 //查询显示
11                 $.ajax({
12                     url: "Shownation.ashx",
13                     data: {name:s},
14                     type: "GET",
15                     dataType: "XML",
16                     success: function (data) {
17                         var cars = $(data).find("car");
18                         $.each(cars, function (key,value) {
19                             //解析XML
20                                 var code = $(value).attr("code");      //读属性
21                                 var name = $(value).find("name").text();   //读子元素
22                                 var prod = $(value).find("prod").text();
23                                 var brand = $(value).find("brand").text();
24                                 var price = $(value).find("price").text();
25                                 //显示在界面上 Html
26                                 var s = "<tr>"
27                                 s += "<td>" + code + "</td>";
28                                 s += "<td>" + name + "</td>";
29                                 s += "<td>" + prod + "</td>";
30                                 s += "<td>" + brand + "</td>";
31                                 s += "<td>" + price + "</td>";
32                                 s += "</tr>";
33 
34                                 $("#dd").append(s);
35                         });
36                         //for (var i = 0; i < cars.length; i++) {
37                         //    //解析XML
38                         //    var code = $(cars).eq(i).attr("code");      //读属性
39                         //    var name = $(cars).eq(i).find("name").text();   //读子元素
40                         //    var prod = $(cars).eq(i).find("prod").text();
41                         //    var brand = $(cars).eq(i).find("brand").text();
42                         //    var price = $(cars).eq(i).find("price").text();
43                         //    //显示在界面上 Html
44                         //    var s = "<tr>"
45                         //    s += "<td>" + code + "</td>";
46                         //    s += "<td>" + name + "</td>";
47                         //    s += "<td>" + prod + "</td>";
48                         //    s += "<td>" + brand + "</td>";
49                         //    s += "<td>" + price + "</td>";
50                         //    s += "</tr>";
51 
52                         //    $("#dd").append(s);
53                         //}
54 
55 
56                     }
57                 });
58             });
59         });
60     </script>
61     <meta charset="utf-8" />
62 </head>
63 <body>
64     <div>
65         名称:<input type="text" name="n" id="n" /> <input type="button" value="查询" name="b" id="b" />
66         <table id="dd" border="1" width="100%"></table>
67 
68     </div>
69 </body>

《Shownation.ashx》

<%@ WebHandler Language="C#" Class="Shownation" %>

using System;
using System.Web;
using System.Linq;
using System.Data.Linq;
public class Shownation : IHttpHandler {
    private MyDataDataContext _Context = new MyDataDataContext();
    public void ProcessRequest (HttpContext context)
    //加载的时候  加载context   操作时 context 打头
    {
        string n = context.Request["name"].ToString();
        //判断出入是否   包含
        var query = _Context.car.Where(p => p.name.Contains(n));
            //Html"<?xml version='1.0'?>"
        context.Response.Write("<?xml version='1.0'?>");
        context.Response.Write("<root>");
         foreach (car data in query)
        {//foreach  便利数据库  一条条  构建
                context.Response.Write("<car code='"+data.code+"'>");
                context.Response.Write("<name>"+data.name+"</name>");
                context.Response.Write("<prod>"+data.brand1.productor.prod_name+"</prod>");
                context.Response.Write("<brand>"+data.brand1.brand_name+"</brand>");
                context.Response.Write("<price>"+data.price+"</price>");
                context.Response.Write("</car>");
        }
        context.Response.Write("</root>");
            context.Response.End();
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

三.时钟

《Third.aspx》

 1 <head runat="server">
 2 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 3     <title></title>
 4     <script src="Script/jquery.js"></script>
 5     <script language="javascript">
 6         ShowTime();
 7 
 8         function ShowTime() {
 9             //...
10             //alert("aaaa");
11             $.ajax({
12                 url: "Second.aspx",
13                 data: {},
14                 type: "POST",
15                 dataType: "Text",
16                 success: function (data) {
17                     $("#Label1").text(data);
18                 }
19             });
20             window.setTimeout("ShowTime()", 1000);
21         }
22     </script>
23 </head>
24 <body>
25     <form id="form1" runat="server">
26     <div>
27     <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="42pt" ForeColor="Red"></asp:Label>
28     </div>
29     </form>
30 </body>

《Second.aspx》

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 
 8 public partial class Second : System.Web.UI.Page
 9 {
10     protected void Page_Load(object sender, EventArgs e)
11     {
12         Response.Write(DateTime.Now.ToString());
13         Response.End();
14     }
15 }
原文地址:https://www.cnblogs.com/hanke123/p/5140919.html