- 第1节:JSON 简介
- 第2节:JSON 语法
- 第3节:JSON 使用
- 第4节:JSONP 教程
- 第5节:JSON 数据类型
- 第6节:JSON 对象
- 第7节:JSON 模式
- 第8节:JSON 与 XML 对比
- 第9节:JSON 基础结构
- 第10节:JSON 数据格式
- 第11节:JSON 数据解析的方法
- 第12节:JSON 数据解析过程详解
- 第13节:JSON 解析URL参数
- 第14节:JSON 元素的添加删除
- 第15节:JSON 数据解析的3种方式
- 第16节:JavaScript 解析Json字符串的性能比较分析代码
- 第17节:JavaScript解析json格式数据简单示例
- 第18节:JSON数据遍历之for-in
- 第19节:jQuery 遍历Json的实现代码
- 第20节:JS遍历Json字符串
- 第21节:直接在JS里创建JSON数据然后遍历使用
- 第22节:JS循环遍历JSON数据的方法
- 第23节:js 遍历json返回的map内容示例代码
- 第24节:JSP+jquery使用ajax方式调用json的实现方法
- 第25节:jquery中的ajax方法怎样通过JSONP进行远程调用
- 第26节:jquery ajax jsonp跨域调用实例代码
- 第27节:jQuery插件jQuery-JSONP开发ajax调用使用注意事项
- 第28节:json的键名为数字时的调用方式
- 第29节:php跨域调用json的例子
- 第30节:jquery中ajax调用json数据的使用说明
- 第31节:ASP.NET(AJAX+JSON)实现对象调用
- 第32节:JavaScript转换与解析JSON方法实例详解
- 第33节:将JavaScript的jQuery库中表单转化为JSON对象的方法
- 第34节:javascript中JSON对象与JSON字符串相互转换实例
- 第35节:JavaScript中字符串(string)转json的方法
- 第36节:JavaScript将XML转成JSON的方法
- 第37节:java与javascript之间json格式数据互转介绍
- 第38节:转义字符(\)对JavaScript中JSON.parse的影响概述
- 第39节:JavaScript中json对象和string对象之间相互转化
- 第40节:javascript中将Object转换为String函数代码 (json str)
- 第41节:转换json格式的日期为Javascript对象的函数
- 第42节:AJAX跨域请求获取JSON数据
- 第43节:JS根据key值获取URL中的参数值
- 第44节:jquery用ajax方式从后台获取json数据后如何将内容填充到下拉列表
- 第45节:js获取json元素数量的方法
- 第46节:JSONP获取Twitter和Facebook文章数的具体步骤
- 第47节:js获取通过ajax返回的map型的JSONArray的方法
- 第48节:JSP中获取ExtJS.Ajax前台传递的JSON数据实现过程
- 第49节:jQuery中使用Ajax获取JSON格式数据示例代码
- 第50节:JQuery 获取json数据$.getJSON方法的实例代码
- 第51节:javascript中JSON对象与JSON字符串相互转换实例
- 第52节:JavaScript中字符串(string)转json的方法
- 第53节:IE6/IE7中JavaScript json提示缺少标识符、字符串或数字问题处理
- 第54节:使用JavaScript构建JSON格式字符串实现步骤
- 第55节:JavaScript 解析Json字符串的性能比较分析代码
- 第56节:javascript巧用eval函数组装表单输入项为json对象的方法
- 第57节:用javascript对一个json数组深度赋值示例
- 第58节:解析javascript 数组(以及json元素的添加删除)
- 第59节:js数组如何添加json数据
- 第60节:js/jquery解析json和数组格式的方法详解
ASP.NET(AJAX+JSON)实现对象调用
客户端
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ASP.NETA_JAX.aspx.cs" Inherits="_Default" %>
<!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 runat="server">
<title></title>
<script type="text/jscript">
function CallServer() {
//JSON发送对象
ServerSum("{name:'linyijia',age:'21'}");
}
function GetRegister(rg, contex) {
document.getElementById("TxtRegister").value=rg;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
用户名:<input id="TxtNum1" type="text" />
<br />
服务器:<input id="TxtRegister" type="text" /><br />
<button id="SumBtn" type="button" onclick="CallServer()">登录</button>
</div>
</form>
</body>
</html>
服务器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;
public partial class _Default : System.Web.UI.Page ,ICallbackEventHandler
{
Users u = null;
protected void Page_Load(object sender, EventArgs e)
{
//回调GetRegister方法
string CallBackFun = Page.ClientScript.GetCallbackEventReference(this,"arg","GetRegister","context");
//创建ServerSum方法,在客户端调用的时候就,会回调GetRegister方法,把参数传给RaiseCallbackEvent(string eventArgument ),最后通过
//GetCallbackResult()方法把返回值传给客户端
string RegisterFun = string.Format("function ServerSum(arg,context){{{0};}}",CallBackFun);
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"ServerSum",RegisterFun,true);
}
string mssage = string.Empty;
#region ICallbackEventHandler 成员
public string GetCallbackResult()
{
return "服务器:你好,你的用户名为:" + u.Name + "你的年龄为" + u.Age;
}
public void RaiseCallbackEvent(string eventArgument)
{
JavaScriptSerializer js = new JavaScriptSerializer();
u =js.Deserialize<Users>(eventArgument);
}
#endregion
}
Users类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
///User 的摘要说明
/// </summary>
public class Users
{
string name;
public string Name
{
get { return name; }
set { name = value; }
}
string age;
public string Age
{
get { return age; }
set { age = value; }
}
}
使用JSON向服务器发送一个对象,服务器通过实现 ICallbackEventHandler接口后,重写GetCallbackResult和RaiseCallbackEvent方法,在回调的时候,在RaiseCallbackEvent方法中反序列化JSON,并在GetCallbackResult把结果返回给客户端.
asp.net中使用jquery+ashx做ajax,json做数据传输
一、准备工作
1.建web应用程序aspnetAjax
2.建index.htm
3.建个js文件夹,把jquery.js放进去,
4.建ajax文件夹,里面放ashx
5.在js文件夹建index.js,一般我们都是一个页面对应一个js
6.在ajax文件夹,建IndexHandler.ashx,一般一个js页面对应一个一般用户控件,这样层次感很强,也很好维护。
二、html页面
html页面就简单了,我们要用ajax读后台做个下拉列表,所以页面就放个DIV就行了。其他的交给js.
<!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 src="js/jquery-1.2.3-intellisense.js" type="text/javascript"></script>
<script src="js/index.js" type="text/javascript"></script>
</head>
<body>
企业性质<div id="vocaspec"> </div>
行业类型<div id="industr"> </div>
</body>
</html>
编写IndexHandler.ashx代码
namespace aspnetAjax.ajax
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
public class IndexHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
//接收提交过来的meth参数
string meth = context.Request.Params["meth"].ToString();
//根据参数调用不同的方法
switch (meth)
{
case "load":
loadjson(context);
break;
case "add":
add(context);
break;
}
}
public bool IsReusable
{
get
{
return false;
}
}
//页面加载方法,调用BLL,获得数据
private void loadjson(HttpContext context)
{
//实例BLL
VocaSpecSortBLL vocaSpec = new VocaSpecSortBLL();
BLL.Owner ownerbll = new GYXMGL.BLL.Owner();
DataSet ds = vocaSpec.GetList("");
DataSet dsindustr = ownerbll.Getcharcte();
//实例一个StringBuilder 用来拼接一个json数据
StringBuilder sbvoca = new StringBuilder();
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
sbvoca.Append("{\"voce\":[");
int i = 1;
int count = ds.Tables[0].Rows.Count;
foreach (DataRow dr in ds.Tables[0].Rows)
{
if (i == count)
{
sbvoca.Append("{\"code\":\"" + dr[0] + "\",\"name\":\"" + dr[1] + "\"}");
}
else
{
sbvoca.Append("{\"code\":\"" + dr[0] + "\",\"name\":\"" + dr[1] + "\"},");
}
i++;
}
sbvoca.Append("]");
}
if (dsindustr != null && dsindustr.Tables[0].Rows.Count > 0)
{
sbvoca.Append(",\"industr\":[");
int i = 1;
int count = dsindustr.Tables[0].Rows.Count;
foreach (DataRow dr in dsindustr.Tables[0].Rows)
{
if (i == count)
{
sbvoca.Append("{\"code\":\"" + dr[0] + "\",\"name\":\"" + dr[1] + "\"}");
}
else
{
sbvoca.Append("{\"code\":\"" + dr[0] + "\",\"name\":\"" + dr[1] + "\"},");
}
i++;
}
sbvoca.Append("]");
}
sbvoca.Append("}");
context.Response.Write(sbvoca.ToString());
context.Response.End();
}
}
}
我们把index.js改下
$(document).ready(function() {
$.ajax({
type: "POST",
url: "../ajax/NewOwnerHandler.ashx",
//我们用text格式接收
dataType: "text",
data: "meth=load",
success: function(msg) {
alert(msg);
//显示后台数据
$("#vocaspec").html(msg);
// $("#industr").html(industr);
}
});
});
看到如下数据,就是ashx中response给我们的json格式数据,现在我们要把这些数据
显示在下拉列表中。就要遍历json中的数组。
{
"voce":[{"code":"1","name":"农林水利"},{"code":"10","name":"军工"},{"code":"11","name":"农林"},{"code":"12","name":"水利(电)"},{"code":"13","name":"水电站"},{"code":"14","name":"输变线"},{"code":"15","name":"煤矿"},{"code":"16","name":"气田"},{"code":"17","name":"公路"},{"code":"18","name":"铁路"},{"code":"19","name":"民航"},{"code":"2","name":"能源"},{"code":"20","name":"信息产业"},{"code":"21","name":"化工"},{"code":"22","name":"机械"},{"code":"23","name":"冶金"},{"code":"24","name":"有色金属"},{"code":"25","name":"建材"},{"code":"26","name":"医药"},{"code":"27","name":"轻工"},{"code":"28","name":"农牧产品深加工"},{"code":"3","name":"交通"},{"code":"4","name":"通讯"},{"code":"5","name":"特色产业"},{"code":"6","name":"城市基础设施"},{"code":"7","name":"商贸流通"},{"code":"8","name":"旅游"},{"code":"9","name":"文体卫"}],
"industr":[{"code":"1","name":"国有"},{"code":"2","name":"私人"}]
}
修改index.js代码,遍历json数据把数据显示成下拉列表
$(document).ready(function() {
$.ajax({
type: "POST",
url: "../ajax/NewOwnerHandler.ashx",
//json格式接收数据
dataType: "json",
//指点后台调用什么方法
data: "meth=load",
success: function(msg) {
//实例2个字符串变量来拼接下拉列表
var industr = "<select name=\"industr\"><option label=\"---请选择---\"></option>";
var vocaspec = "<select name=\"vocaspec\"><option label=\"---请选择---\"></option>";
//使用jquery解析json中的数据
$.each(msg.voce, function(n, value) {
vocaspec += ("<option value=\"" + value.code + "\" label=\"" + value.name + "\">");
vocaspec += ("</option>");
});
$.each(msg.industr, function(n, value) {
industr += ("<option value=\"" + value.code + "\" label=\"" + value.name + "\">");
industr += ("</option>");
});
industr += ("</select>");
$("#vocaspec").html(vocaspec);
$("#industr").html(industr);
}
});
});
这个实例涉及到的知识点
1、使用一般处理程序,接收request。并可以使用response数据
string meth = context.Request.Params["meth"].ToString();
因为一般处理程序
public class IndexHandler : IHttpHandler
他实现IHttpHandler接口
2、json数据格式
3、使用jquery ajax,并用jquery解析json数据。