一个DBhelper类,用来操作数据库

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace JsonTest.helper
{
    public class DBhelper
    {
        public static string ConnectStrings;

public static void GetConnectStr()
        {
            ConnectStrings = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStr"].ConnectionString;
        }
        public static SqlDataReader GetCommentByID(int id,string sql)
        {
            GetConnectStr();
            SqlDataReader rs = null;
            try
            {
                SqlConnection con = new SqlConnection(ConnectStrings);
                con.Open();
                SqlCommand com = new SqlCommand(sql, con);
                rs=com.ExecuteReader();
            }
            catch (Exception err)
            {
                throw err;
            }
            return rs;
        }
    }
};

前台htm页发出ajax post请求

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript" src="Scripts/jquery-1.4.1.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#button1").click(function sys() {
                $.post("html_1.ashx", { "ID": 1 }, function sys(json) {
                    var str = json.item1 + json.item2 + json.item3 + json.item4;
                    $("ul").append("<li>" + str + "</li>");
                }, "json");
            });
        });        
    </script>
</head>
<body>
    <input type="button" id="button1" value="点击" />
    <ul>
    </ul>
</body>
</html>

后台ashx处理程序处理请求

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using JsonTest.helper;
using System.Web.Script.Serialization;

namespace JsonTest
{  
    /// <summary>
    /// html_1 的摘要说明
    /// </summary>
    public class html_1 : IHttpHandler
    {

public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int id = Convert.ToInt32(context.Request["ID"]);
            string sql = "select * from item where ItemID=" + id + "";
            SqlDataReader sdr = null;
            sdr=DBhelper.GetCommentByID(id, sql);
            item Item = new item();
            while (sdr.Read())
            {

Item.item1 = sdr[0].ToString();
                Item.item2 = sdr[1].ToString();
                Item.item3 = sdr[2].ToString();
                Item.item4 = sdr[3].ToString();
            }
            sdr.Close();
            sdr = null;
            JavaScriptSerializer js = new JavaScriptSerializer();
            string json = js.Serialize(Item).ToString();
            context.Response.Write(json);
        }

public bool IsReusable
        {
            get
            {
                return false;
            }
        }

public class item
        {
            public string item1;
            public string item2;
            public string item3;
            public string item4;
        }
    }
}

这样就可以无刷新的实现

简单的ajax获取json的更多相关文章

  1. qt qml ajax 获取 json 天气数据示例

    依赖ajax.js类库,以下代码很简单的实现了获取天气json数据并展示的任务 [TestAjax.qml] import QtQuick 2.0 import "ajax.js" ...

  2. jQuery中使用Ajax获取JSON格式数据示例代码

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.JSONM文件中包含了关于“名称”和“值”的信息.有时候我们需要读取JSON格式的数据文件,在jQuery中 ...

  3. ajax获取json对象

    ajax获取json对象 ajax获取json数据,都是一个原理,设置response 的Content-Type:application/json,这样浏览器自动会解析为json对象 $result ...

  4. Jquery 模板插件 jquery.tmpl.js 的使用方法(1):基本语法,绑定,each循环,ajax获取json数据

    jquery.tmpl.js 是一个模板js  ,主要有2个方法 (1):$.template()方法,将一段script或者是Html编译为模板,例如 $.template('myTemplate' ...

  5. 通过Jquery中Ajax获取json文件数据

    1. JSON(JavaScript Object Notation): javaScript对象表示法: 是存储和交换文本信息的语法,比xml更小,更快,更易解析. 2. JSON基本书写格式 : ...

  6. Ajax获取Json多个集合并同时遍历

    Ajax获取Json多个集合并同时遍历: 方法一.:将多个集合放入MAP集合. 后台:Servlet @Override protected void doPost(HttpServletReques ...

  7. Ajax获取 Json文件提取数据

    摘自 Ajax获取 Json文件提取数据 1. json文件内容(item.json) [ { "name":"张国立", "sex":&q ...

  8. ajax获取json数据及实现跨域请求

    最近想练习一下ajax获取json数据 , 首先上网找一些在线的可用来测试的接口. -----------------------------------------------------这里是接口 ...

  9. JS-利用ajax获取json数据,并传入页面生成动态tab

    封装好的:ajax.js function ajax(url, fnSucc,fnFaild){ //1[创建] if(window.XMLHttpRequest){ var oAjax = new ...

随机推荐

  1. HDU 3923 Invoker 【裸Polya 定理】

    参考了http://blog.csdn.net/ACM_cxlove?viewmode=contents           by---cxlove 的模板 对于每一种染色,都有一个等价群,例如旋转, ...

  2. POJ 1830 【高斯消元第一题】

    首先...使用abs()等数学函数的时候,浮点数用#include<cmath>,其它用#include<cstdlib>. 概念: [矩阵的秩] 在线性代数中,一个矩阵A的列 ...

  3. Struts2、spring2、hibernate3在SSH中各起什么作用

    简单的说: struts 控制用的 hibernate 操作数据库的 spring用解耦的 详细的说: STRUTS 在 SSH 框架中起控制的作用 , 其核心是 Controller, 即 Acti ...

  4. HDU 4814 Golden Radio Base 小模拟

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4814 题意:黄金比例切割点是,如今要求把一个10进制的的数转化成一个phi进制的数,而且不能出现'11'的 ...

  5. 三、IF...ELSE和缩进

    IF...ELSE和缩进 根据用户输入的不同做不同的事情 注意语法结尾的冒号. 例1: name = input("Please input your name:") if nam ...

  6. 转载python并行运算实例

    Python的并发处理能力臭名昭著.先撇开线程以及GIL方面的问题不说,我觉得多线程问题的根源不在技术上而在于理念.大部分关于Pyhon线程和多进程的资料虽然都很不错,但却过于细节.这些资料讲的都是虎 ...

  7. 标准I/O库函数的缺陷

    标准IO库存在着两个缺陷.首先,它是在某一台特定类型的机器上实现的,而没有考虑其他的具有不同特性的机器.这样就可能导致在其中的一台机器上运行很好的程序,在另外一台机器上却出现错误,而这其中的原因仅仅是 ...

  8. 在Android开发中,定时器一般有以下3种实现方法

    在Android开发中,定时器一般有以下3种实现方法: 原文地址http://www.360doc.com/content/12/0619/13/87000_219180978.shtml 一.采用H ...

  9. JVM调优总结(十二)-参考资料

    能整理出上面一些东西,也是因为站在巨人的肩上.下面是一些参考资料,供大家学习,大家有更好的,可以继续完善:) · Java 理论与实践: 垃圾收集简史 · Java SE 6 HotSpot[tm] ...

  10. 基于visual Studio2013解决算法导论之011快排改良

     题目 快排改良 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <malloc.h> #in ...