zoukankan      html  css  js  c++  java
  • json+一般处理程序读取数据库数据

    一般处理程序的语法结构

      string jsoncallback = context.Request["jsoncallback"]; 声明变量 前台传值使用
            string josn = "([";  定义变量接受值
            context.Response.ContentType = "text/json";
            Cx cc = new Cx();  后台查询的类方法
            List<Model> list = new List<Model>();  所用到的实体类
            list = cc.Chaxun();   返回查询出的数据
            foreach (Model item in list)   遍历数据 
            {
                josn += "{"id":""+item.ID+"","title":""+item.Title+""},";       此机构是json的固定格式,必须这样写
            }
            josn = josn.Substring(0, josn.Length - 1) + "])";     截取上面json最后的那个逗号
            context.Response.Write(jsoncallback + josn);   输入

    前台调用一般处理程序并解析json

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="json_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="../jquery-1.8.2.min.js"></script>
        <script>
            var htmls = "";
            $(document).ready(function () {
                $.ajax({
                    type: "post",
                    dataType: "json",
                    url: "Handler.ashx?&jsoncallback=?",  
                    success: function(data) {
                        if (data!=null) {
                            $(data).each(function (i, obj) {
                                htmls += obj.title;
                            });
                            $("#bd").html(htmls);
                        }
                    }
                });
            });

        </script>

    </head>
    <body>
        <form>
            <div id="bd">
            </div>
        </form>
    </body>
    </html>

  • 相关阅读:
    设置tomcat访问根路径
    关于Java抽象类的理解
    JavaIO
    synchronized关键字
    Java线程池
    Codeforces1478F-Nezzar and Nice Beatmap
    Codeforces1477B-Nezzar and Binary String
    Codeforces1476D Journey
    Codeforces1478D Nezzar and Board
    Codeforces Round #697 (Div. 3)G. Strange Beauty
  • 原文地址:https://www.cnblogs.com/loveYN/p/4531813.html
Copyright © 2011-2022 走看看