2018-07-05关于ajax的随笔

Posted zqyyx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
        }
    }

 

以上是关于2018-07-05关于ajax的随笔的主要内容,如果未能解决你的问题,请参考以下文章

Spring MVC 3.2 Thymeleaf Ajax 片段

经济随笔

html PHP代码片段: - AJAX基本示例:此代码演示了使用PHP和JavaScript实现的基本AJAX功能。

7-11随笔

AJAX相关JS代码片段和部分浏览器模型

AJAX学习随笔