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>

  • 相关阅读:
    WinSCP 与 Putty 中文显示乱码解决方法
    centos 6.2上oracle 11g的远程安装
    CentOs 6.3_64静默安装oracle11g_r2
    IP地址修改后ORACLE不能使用问题
    linux查看本机IP、gateway、dns
    linux启动SSH及开机自动启动
    linux下如何从自动获取ip转到手动配置ip
    Linux系统(CentOS 6.4)的NTFS驱动NTFS3g的安装和配置
    正则表达式——去除文本中的非汉字(VB2005)
    遍历排列的实现——VB2005
  • 原文地址:https://www.cnblogs.com/loveYN/p/4531813.html
Copyright © 2011-2022 走看看