ajax 请求 对json传的处理 Jquery 使用Ajax获取后台返回的Json数据后,页面处理
- <!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.8.0.min.js" type="text/javascript"></script>
- <script type="text/javascript">
- $(function () {
- $.ajax({
- url: 'jsondata.ashx',
- type: 'GET',
- dataType: 'json',
- timeout: 1000,
- cache: false,
- beforeSend: LoadFunction, //加载执行方法
- error: erryFunction, //错误执行方法
- success: succFunction //成功执行方法
- })
- function LoadFunction() {
- $("#list").html('加载中...');
- }
- function erryFunction() {
- alert("error");
- }
- function succFunction(tt) {
- $("#list").html('');
- //eval将字符串转成对象数组
- //var json = { "id": "10086", "uname": "zhangsan", "email": "zhangsan@qq.com" };
- //json = eval(json);
- //alert("===json:id=" + json.id + ",uname=" + json.uname + ",email=" + json.email);
- var json = eval(tt); //数组
- $.each(json, function (index, item) {
- //循环获取数据
- var name = json[index].Name;
- var idnumber = json[index].IdNumber;
- var sex = json[index].Sex;
- $("#list").html($("#list").html() + "<br>" + name + " - " + idnumber + " - " + sex + "<br/>");
- });
- }
- });
- </script>
- </head>
- <body>
- <ul id="list">
- </ul>
- </body>
- </html>
- <%@ WebHandler Language="C#" Class="jsondata" %>
- using System;
- using System.Web;
- using System.Web.Script.Serialization;
- using System.IO;
- using System.Text;
- using System.Collections.Generic;
- using Newtonsoft.Json;
- using System.Data;
- public class jsondata : IHttpHandler {
- public void ProcessRequest(HttpContext context)
- {
- context.Response.ContentType = "text/plain";
- string JsonStr = JsonConvert.SerializeObject(CreateDT());
- context.Response.Write(JsonStr);
- context.Response.End();
- }
- #region 创建测试数据源
- //创建DataTable
- protected DataTable CreateDT()
- {
- DataTable tblDatas = new DataTable("Datas");
- //序号列
- //tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
- //tblDatas.Columns[0].AutoIncrement = true;
- //tblDatas.Columns[0].AutoIncrementSeed = 1;
- //tblDatas.Columns[0].AutoIncrementStep = 1;
- //数据列
- tblDatas.Columns.Add("IdNumber", Type.GetType("System.String"));
- tblDatas.Columns.Add("Name", Type.GetType("System.String"));
- tblDatas.Columns.Add("BirthDate", Type.GetType("System.String"));
- tblDatas.Columns.Add("Sex", Type.GetType("System.String"));
- tblDatas.Columns.Add("Wage", Type.GetType("System.Decimal"));
- tblDatas.Columns.Add("Bonus", Type.GetType("System.Decimal"));
- //统计列开始
- tblDatas.Columns.Add("NeedPay", Type.GetType("System.String"), "Wage+Bonus");
- //统计列结束
- tblDatas.Columns.Add("Address", Type.GetType("System.String"));
- tblDatas.Columns.Add("PostCode", Type.GetType("System.String"));
- //设置身份证号码为主键
- tblDatas.PrimaryKey = new DataColumn[] { tblDatas.Columns["IdNumber"] };
- tblDatas.Rows.Add(new object[] { "43100000000000", "张三", "1982", "0", 3000, 1000, null, "深圳市", "518000" });
- tblDatas.Rows.Add(new object[] { "43100000000001", "李四", "1983", "1", 3500, 1200, null, "深圳市", "518000" });
- tblDatas.Rows.Add(new object[] { "43100000000002", "王五", "1984", "1", 4000, 1300, null, "深圳市", "518000" });
- tblDatas.Rows.Add(new object[] { "43100000000003", "赵六", "1985", "0", 5000, 1400, null, "深圳市", "518000" });
- tblDatas.Rows.Add(new object[] { "43100000000004", "牛七", "1986", "1", 6000, 1500, null, "深圳市", "518000" });
- return tblDatas;
- }
- #endregion
- public bool IsReusable
- {
- get
- {
- return false;
- }
- }
- }
- <!--
- <script type="text/javascript">
- $(function () {
- $.ajax({
- url: 'jsondata.ashx',
- type: 'GET',
- dataType: 'json',
- timeout: 1000,
- cache: false,
- beforeSend: LoadFunction, //加载执行方法
- error: erryFunction, //错误执行方法
- success: succFunction //成功执行方法
- })
- function LoadFunction() {
- $("#list").html('加载中...');
- }
- function erryFunction() {
- alert("error");
- }
- function succFunction(tt) {
- $("#list").html('');
- //eval将字符串转成对象数组
- //var json = { "id": "10086", "uname": "zhangsan", "email": "zhangsan@qq.com" };
- //json = eval(json);
- //alert("===json:id=" + json.id + ",uname=" + json.uname + ",email=" + json.email);
- var json = eval(tt); //数组
- $.each(json, function (index, item) {
- //循环获取数据
- var Key = json[index].key;
- var Info = json[index].info;
- // var idnumber = json[index].IdNumber;
- // var sex = json[index].Sex;
- $("#list").html($("#list").html() + "<br>" + Key + "----" + Info.name); //+ " - " + idnumber + " - " + sex + "<br/>");
- });
- }
- });
- </script>
- -->
- <%@ WebHandler Language="C#" Class="jsondata" %>
- using System;
- using System.Web;
- using System.Web.Script.Serialization;
- using System.IO;
- using System.Text;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data;
- public class jsondata : IHttpHandler {
- public void ProcessRequest(HttpContext context)
- {
- context.Response.ContentType = "text/plain";
- context.Response.Cache.SetNoStore();
- string data = "[{\"key\":\"1\",\"info\":{\"name\":\"222\",\"age\":\"333\",\"sex\":\"444\"}},{\"key\":\"2\",\"info\":{\"name\":\"999\",\"age\":\"000\",\"sex\":\"111\"}}]";
- context.Response.Write(new JavaScriptSerializer().Serialize(data));
- }
- public bool IsReusable
- {
- get
- {
- return false;
- }
- }
- }
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test2013.aspx.cs" Inherits="Test2013" %>
- <!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 src="JS/jquery-1.8.0.min.js" type="text/javascript"></script>
- <script type="text/javascript">
- function GetPara(o) {
- var sortid = $(o).val();
- $.ajax({
- url: 'GetPara.ashx?type=get&sortid=' + sortid,
- type: 'GET',
- dataType: 'json',
- timeout: 3000,
- cache: false,
- beforeSend: LoadFunction, //加载执行方法
- error: erryFunction, //错误执行方法
- success: succFunction //成功执行方法
- })
- function LoadFunction() {
- $("#list").html('加载中...');
- }
- function erryFunction() {
- alert("error");
- }
- function succFunction(tt) {
- $("#list").html('');
- var json = eval(tt); //数组
- $.each(json, function (index, item) {
- //循环获取数据
- var Id = json[index].id;
- var Name = json[index].name;
- $("#list").html($("#list").html() + "<br>" + Name + "<input type='text' id='" + Id + "' /><br/>");
- });
- }
- };
- function SavePara() {
- var parameter = {};
- $("#list input:text").each(function () {
- var key = $(this).attr("id");
- var value = $(this).val();
- parameter[key] = value;
- });
- $.ajax({
- url: 'GetPara.ashx?type=save',
- type: 'POST',
- dataType: 'json',
- data: parameter,
- timeout: 3000,
- cache: false,
- beforeSend: LoadFunction, //加载执行方法
- error: erryFunction, //错误执行方法
- success: succFunction //成功执行方法
- })
- function LoadFunction() {
- }
- function erryFunction() {
- }
- function succFunction(tt) {
- }
- };
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:DropDownList ID="ddl1" runat="server" onchange="GetPara(this)">
- </asp:DropDownList>
- <ul id="list"></ul>
- <input type="button" value="保存数据" onclick="SavePara()" />
- </div>
- </form>
- </body>
- </html>
- <%@ WebHandler Language="C#" Class="GetPara" %>
- using System;
- using System.Web;
- using System.Data;
- using System.Collections.Generic;
- using System.Web.Script.Serialization;
- public class GetPara : IHttpHandler {
- public void ProcessRequest (HttpContext context) {
- context.Response.ContentType = "text/plain";
- string SortId = context.Request["sortid"];
- string Type = context.Request["type"];
- if (Type=="get")
- {
- if (!string.IsNullOrEmpty(SortId))
- {
- DataTable dt = MSCL.SqlHelper.GetDataTable("select * from PR_PRODUCTPARAS where sortid='" + SortId + "' ");
- List<Paras> list = new List<Paras>();
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- Paras a = new Paras();
- a.id = dt.Rows[i]["PARAID"].ToString();
- a.name = dt.Rows[i]["PARANAME"].ToString();
- list.Add(a);
- }
- context.Response.Write(new JavaScriptSerializer().Serialize(list));
- }
- }
- else if (Type == "save")
- {
- //反序列化json
- System.IO.Stream stream = context.Request.InputStream;
- System.IO.StreamReader sr = new System.IO.StreamReader(stream, System.Text.Encoding.GetEncoding("UTF-8"));
- string sJson = sr.ReadToEnd();
- if (sJson.Contains("&"))
- {
- string[] sArr = sJson.Split('&');
- for (int i = 0; i < sArr.Length; i++)
- {
- string[] sArr1 = sArr[i].Split('=');
- object id = sArr1[0];
- object value = sArr1[1];
- }
- }
- }
- else
- { }
- }
- public bool IsReusable {
- get {
- return false;
- }
- }
- public struct Paras
- {
- public string id;
- public string name;
- }
- }
ajax 请求 对json传的处理 Jquery 使用Ajax获取后台返回的Json数据后,页面处理的更多相关文章
- JS获取后台返回的JSON数据
问题:通过$.get从后台获取了一段json串{"id":"1","name":"ww"},然后要拿到这里面的id和na ...
- Jquery 使用Ajax获取后台返回的Json数据后,页面处理
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 【转】Jquery 使用Ajax获取后台返回的Json数据后,页面处理
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- H5 FormData对象的使用——进行Ajax请求并上传文件
XMLHttpRequest Level2 添加了一个新的接口——FormData .[ 主要用于发送表单数据,但也可以独立使用于传输键控数据.与普通的Ajax相比,它能异步上传二进制文件 ] 利用F ...
- 【前台 ajax】前台ajax请求,地址正确,但是报错不进入后台
前台ajax请求,地址正确,但是报错不进入后台 出现上述问题,可能的情况是 1.ajax用的post,而后台限定用get,或者所有的post请求都被拦截,所以不能正常进入并且报错403 @Reques ...
- js进阶ajax读取json数据(ajax读取json和读取普通文本,和获取服务器返回数据(链接)都是一样的,在url处放上json文件的地址即可)
js进阶ajax读取json数据(ajax读取json和读取普通文本,和获取服务器返回数据(链接)都是一样的,在url处放上json文件的地址即可) 一.总结 ajax读取json和读取普通文本,和获 ...
- 使用form表单提交请求如何获取后台返回的数据?
问题描述 一般的form表单提交是单向的:只能给服务器发送数据,但是无法获取服务器返回的数据,也就是无法读取HTTP应答包. 想要真正的半双工通讯一般需要使用Ajax, 但是Ajax对文件传输也很麻烦 ...
- js循环处理后台返回的json数组
<script type="text/javascript"> function gongdan_search(elm){ var dangqian_value=$(e ...
- 前台如何处理后台返回的json数据
后台返回的json数据格式: { "state": true, "data": { "id": 0, "name": & ...
随机推荐
- <a>每次点击都会让浏览器重新打开一个窗口问题
<a> 标签的 target 属性规定在何处打开链接文档.如果在一个 <a> 标签内包含一个 target 属性,浏览器将会载入和显示用这个标签的 href 属性命名的.名称与 ...
- html/CSS基础知识回顾
html部分 块级元素: 一般用来搭建网站架构,布局,装载内容...像这些大体力的活都属于块级元素.它包括以下标签: address,blockquote,center,dir, div, dl, d ...
- [转载]【基础篇】不为人知的Maya移动坐标轴
maya 设置轴向1.将整体模型方向调整到与世界坐标系一致.设置具体模型的轴朝向. 操作:选中模型,按住W键,鼠标左键,在弹出的菜单中选择Axis,比较常用的有Set To Ponit.Set To ...
- theano + gpu
Teano安装测试 1. Anaconda 安装 Anaconda是一个科学计算环境,自带的包管理器conda很强大.之所以选择它是因为它内置了python,以及numpy.scipy两个必要库和一些 ...
- quartz-2.2.x 快速入门 (1)
欢迎来到quartz快速入门教程.阅读本教程,你将会了解: quartz下载 quartz安装 根据你的需要,配置Quartz 开始一个示例应用 当熟悉了quratz调度的基本功能后,可以尝试一些 ...
- 用JDBC访问MySQL
/* 在数据库中创建一个Employee的类 create table Employee( id int primary key, name varchar(20), age int); */ imp ...
- Java 概述
一 Java 程序的种类 1)Java 小应用程序(Java Applet) — 在Web浏览器中运行(内嵌Java虚拟机) —特定标记 <APPLET CODE="HelloWorl ...
- Windows Store App 全球化:在XAML元素中引用字符串资源
在应用程序中可以通过XAML元素和后台代码两种方式引用资源文件中的字符串资源.本小节先讲述如何在XAML元素中引用字符串资源的相关知识点. 在XAML元素中可以通过使用x:Uid属性来引用资源文件中的 ...
- Java 并发和多线程(二) 多线程的优点 [转]
原文:http://tutorials.jenkov.com/java-concurrency/benefits.html 作者:Jakob Jenkov 翻译:古圣昌 ...
- Java mysql 日期相关
获取当前系统时间和日期并格式化输出: import java.util.Date;import java.text.SimpleDateFormat; public class NowString { ...