【2018-07-05】关于ajax的随笔

html1

<script>
    $(document).ready(function () {
        $("#login").click(function () {
            var n = $("#name").val();
            var p = $("#pwd").val();
            $.post("/WebForm1.aspx" , { "name": n, "pwd": p }, dataCallBack);
        });
        dataCallBack = function (rsp) {
            switch (rsp)
            {
                case "1":
                    sessionStorage["USER"] = $("#name").val();
                    window.open("/HtmlPage2.html", "_self");                
                    break;
                case "2":
                    $("#t").css("visibility", "visible");
                    $("#t").text("用户名错误!");
                    $("#name").val("");
                    $("#pwd").val("");
                    break;
                case "3":
                    $("#t").css("visibility", "visible");
                    $("#t").text("密码错误!");
                    $("#pwd").val("");
                    break;
                default:
                    alert("系统出错,请稍后重试!");
            }
        }

   
    });
</script>

aspx1

protected void Page_Load(object sender, EventArgs e)
        {
            Response.Clear();
            string name = Request.Form["name"].Trim();
            string pwd = Request.Form["pwd"].Trim();
            string str1 = "select count(*) from Users where Uname='" + name + "'";
            string strSQL = System.Configuration.ConfigurationManager.AppSettings["SQL"];
            SqlConnection conn = new SqlConnection(strSQL);
            conn.Open();
            SqlCommand cmd1 = new SqlCommand(str1, conn);
            int s1 = (int)cmd1.ExecuteScalar();
            if (s1 < 1)
                Response.Write("2");
            else
            {
                string str2 = "select count(*) from Users where Uname='" + name + "' and Upwd='" + pwd + "'";
                SqlCommand cmd2 = new SqlCommand(str2, conn);
                int s2 = (int)cmd2.ExecuteScalar();
                if (s2 < 1)
                    Response.Write("3");
                else
                    Response.Write("1");
            }         
            Response.End();
        }

html2

<body>
    <div>
        <p id="nh">你好,</p>
    </div>
    <div>
        <table border="1">
            <thead>
                <tr>
                    <th>name</th>
                    <th>password</th>
                </tr>
            </thead>
            <tbody id="tb">                         
           </tbody>
        </table>
    </div>
</body>
</html>
<script>
    $(document).ready(function () {
        $(function () {
            $("#nh").append(sessionStorage.getItem("USER"));
            $.ajax({
                url: "/WebForm2.aspx",
                data: {},
                type: "post",
                dataType: "json",
                success: function (rsp) {
                    var count = Object.keys(rsp).length;
                    for (var i = 0; i < count; i++)
                    {
                        var str = "<tr >  <th>" + rsp[i].name + "</th>";
                        str += "<th>" + rsp[i].pwd + "</th>  </tr>";
                        $("#tb").append(str);
                    }
                },
                error: function () {
                    alert("err");
                }
            });
        });
    });
</script>

aspx2

 protected void Page_Load(object sender, EventArgs e)
        {
            Response.Clear();
            string str = "select * from Users";
            string strSQL = System.Configuration.ConfigurationManager.AppSettings["SQL"];
            SqlConnection conn = new SqlConnection(strSQL);
            conn.Open();
            SqlCommand cmd = new SqlCommand(str, conn);
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            DataSet d = new DataSet();
            da.Fill(d);
            string json="[";
            int count = d.Tables[0].Rows.Count;    
            int douhao=0;
            for (int i = 0; i < count; i++)
            {
                json += "{\"name\":\""+d.Tables[0].Rows[i][0]+"\",\"pwd\":\""+d.Tables[0].Rows[i][1]+"\"}";
                if (douhao < count - 1)
                {
                    json += ",";
                    douhao++;
                }
            }
            json += "]";
            Response.Write(json);
            Response.End();
        }
    }

猜你喜欢

转载自www.cnblogs.com/zqyyx/p/9267659.html