简单的ajax获取json
一个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的更多相关文章
- qt qml ajax 获取 json 天气数据示例
依赖ajax.js类库,以下代码很简单的实现了获取天气json数据并展示的任务 [TestAjax.qml] import QtQuick 2.0 import "ajax.js" ...
- jQuery中使用Ajax获取JSON格式数据示例代码
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.JSONM文件中包含了关于“名称”和“值”的信息.有时候我们需要读取JSON格式的数据文件,在jQuery中 ...
- ajax获取json对象
ajax获取json对象 ajax获取json数据,都是一个原理,设置response 的Content-Type:application/json,这样浏览器自动会解析为json对象 $result ...
- Jquery 模板插件 jquery.tmpl.js 的使用方法(1):基本语法,绑定,each循环,ajax获取json数据
jquery.tmpl.js 是一个模板js ,主要有2个方法 (1):$.template()方法,将一段script或者是Html编译为模板,例如 $.template('myTemplate' ...
- 通过Jquery中Ajax获取json文件数据
1. JSON(JavaScript Object Notation): javaScript对象表示法: 是存储和交换文本信息的语法,比xml更小,更快,更易解析. 2. JSON基本书写格式 : ...
- Ajax获取Json多个集合并同时遍历
Ajax获取Json多个集合并同时遍历: 方法一.:将多个集合放入MAP集合. 后台:Servlet @Override protected void doPost(HttpServletReques ...
- Ajax获取 Json文件提取数据
摘自 Ajax获取 Json文件提取数据 1. json文件内容(item.json) [ { "name":"张国立", "sex":&q ...
- ajax获取json数据及实现跨域请求
最近想练习一下ajax获取json数据 , 首先上网找一些在线的可用来测试的接口. -----------------------------------------------------这里是接口 ...
- JS-利用ajax获取json数据,并传入页面生成动态tab
封装好的:ajax.js function ajax(url, fnSucc,fnFaild){ //1[创建] if(window.XMLHttpRequest){ var oAjax = new ...
随机推荐
- BAAS
http://blogs.embarcadero.com/sarinadupont/category/baas-tutorials/?cid=701G0000000vH0A&elq=51f98 ...
- JavaScript 中的日期和时间
前言 本篇的介绍涵盖以下部分: 1. 时间标准指的是什么?UCT和GMT 的概念.关联和区别? 2. 时间表示标准有哪些? 3. JS 中时间的处理 日期时间标准 日期的标准就不多说了 -- 公元纪年 ...
- 安装虚拟机时出现The Microsoft Runtime DLL
参考文档: http://zhidao.baidu.com/link?url=1E4vr6ToPGm_kAZw4voOqzrPtzGaSIqy3kvcGXehs3KJAkirNKOHJbrsxec3f ...
- 动画api说明
1.Animation的API参考文档:http://blog.csdn.net/harvic880925/article/details/40117115 2.动画插值器的参考: http://bl ...
- Smarty模板Windows下写代码 放到CentOS6.5无法正确解析
如题:报错: Unable to load template file 'System/header.htm' in '/var/www/website/cms/Template/Default/We ...
- TCP拥塞控制算法内核实现剖析(十)
内核版本:3.2.12 主要源文件:linux-3.2.12/ net/ ipv4/ tcp_veno.c 主要内容:Veno的原理和实现 Author:zhangskd @ csdn blog 概要 ...
- SQL Server截取字符串和处理中文技巧
一 环境介绍 SQL Server PRINT @@VERSION MicrosoftSQLServer2012-11.0.2100.60(X64) Feb10201219:39:15 Copyri ...
- javascript真的是异步的吗?且看setTimeout的实现原理以及setTimeout(0)的使用场景
在今天之前我一直以为setTimeout这个函数是异步的,无意中看到了一篇关于setTimeout的文章.发现自己曾经的认识全是错误的,赶紧总结下. 先看一段代码: var start = new D ...
- Java 螺纹第三版 第三章数据同步 读书笔记
多线程间共享数据问题 一.Synchronizedkeyword atomic一词与"原子"无关,它以前被觉得是物质的最小的单元,不能再被拆解成更小的部分. 当 ...
- Python 30分钟入门——数据类型 & 控制结构
Python是一门脚本语言,我也久闻大名,但正真系统的接触学习是在去年(2013)年底到今年(2014)年初的时候.不得不说的是Python的官方文档相当齐全,假设你是在Windows上学习Pytho ...