json前后端交互

2019-03-13  本文已影响1人  青木川_

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="myselect.aspx.cs" Inherits="WebTest.Demos.myselect" %>

<!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>

    <style type="text/css">

        * {

            margin: 0;

            padding: 0;

        }

        .main {

            width: 100%;

            height: auto;

            border: 1px solid #DEDEDE;

        }

        .main_Ul {

            float: left;

            width: 100%;

            height: auto;

            border: 1px solid #DEDEDE;

            list-style: none;

        }

            .main_Ul li {

                float: left;

                width: 15%;

                height: 25px;

                border-right: 1px solid #DEDEDE;

            }

    </style>

</head>

<body>

    <form id="form1" runat="server">

        <input type="button" value="查询" onclick="sele()" />

        <div class="main">

        </div>

    </form>

    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>

    <script type="text/javascript">

        function sele() {

            $.ajax({

                type: "post",

                dataType: "json",

                data: { "method": "selectData" },

                url: "ashx/test.ashx",

                success: function (data) {

                    if (data != null) {

                        var html = "";

                        var book = eval(data);

                        console.log(book.length);

                        for (var i = 0; i < book.length; i++)

                        {

                            html += "<ul class='main_Ul'><li>" + book[i].name + "</li><li>" + book[i].sch_id + "</li><li>" + book[i].sex + "</li><li>" + book[i].isOk + "</li></ul>";

                        }

                        $(".main").html(html);

                    }

                }

            });

        }

    </script>

</body>

</html>

后台

public void ProcessRequest(HttpContext context)

        {

            context.Response.ContentType = "text/plain";

            string method = context.Request.Form["method"];

            var JsonString = string.Empty;

            if (method == "selectData")

            {

                using (var db = SugarDao.GetInstance())

                {

                    List<Student> student = db.Queryable<Student>().ToList();

                    #region 查询数据

                    JsonString = "[";

                    //lambda写法

                    for (int i = 0; i < student.Count; i++)

                    {

                        JsonString += "{";

                        JsonString += "\"name\":\"" + student[i].name + "\",\"sch_id\":\"" + student[i].sch_id + "\",\"sex\":\"" + student[i].sex + "\",\"isOk\":\"" + student[i].isOk + "\"";

                        JsonString = (i == student.Count-1) ? JsonString += "}" : JsonString += "},";

                    }

                    JsonString += "]";

                    #endregion

                    object jsonob = JsonConvert.SerializeObject(JsonString);

                    context.Response.Write(jsonob);

                }

            }

上一篇 下一篇

猜你喜欢

热点阅读