简单的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 ...
随机推荐
- Ural 1086 - Cryptography
While preparing this problem set the jury has run into the following problem: it was necessary to se ...
- Chapter 14 观察者模式
观察者模式又叫做发布-订阅模式:定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象.这个主题对象在状态发生变化时,会通知所有观察者对象,使它们能够自动更新自己. 观察者模式所做的工作其 ...
- 《HTML 5网页开发实例具体解释》文件夹
第一篇 从宏观上认识HTML 5 讲述了HTML 5引发的Web革命.HTML 5的总体特性.HTML 5相关概念和框架和开发环境搭建. 第1章 HTML 5引发的Web革命 1.1 你是不是真的 ...
- XP里面其实也讲究admin的执行权限
错误的方法:比如说,当前登录帐号cliff是管理员,此时直接运行cmd,输入: net user administrator 123 结果说这个用户找不到. --------------------- ...
- Robot Framework: 自定义自己的python库
利用Robot Framework编写测试用例,往往需要开发自己的关键字,有的关键字需要通过自己编写python代码来实现.这在rf中,就需要自己定义python库.这个过程其实不复杂,本文来介绍下. ...
- MDCC为移动开发者服务:一看、一聊、一聚
MDCC为移动开发者服务:一看.一聊.一聚-CSDN.NET MDCC为移动开发者服务:一看.一聊.一聚 发表于2013-11-05 20:54| 2698次阅读| 来源CSDN| 6 ...
- ESRI Shapefiles (SHP)
ESRI Shapefiles (SHP) Also known as ESRI ArcView Shapefiles or ESRI Shapefiles. ESRI is the company ...
- Ext的异步请求(二级级联动态加载下拉列表)
页面: <tr> <td class="label" width="300" >作业计划项模板</td> <td> ...
- cocos2d-x on wp8架构简单介绍
1,基于C++的开发架构 支持3大移动平台以及3大桌面平台. 分为图形,声音,物理3大模块,另外还有脚本的导出. 在wp8/win32上的图形是基于d3d的,而在其它平台是基于opengl/openg ...
- FileStream -- 复制文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...