您的位置:首页 > 数据库

json+一般处理程序读取数据库数据

2014-09-02 09:57 295 查看
一般处理程序的语法结构

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐