object inpu empty 获取 reader form 取数 oid
- "-//W3C//DTD Xhtml 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">"http://www.w3.org/1999/xhtml">
- "JS/jquery-1.8.0.min.js"type="text/javascript">"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": "[email protected]" };
- //json = eval(json);
- //alert("===json:id=" + json.id + ",uname=" + json.uname + ",email=" + json.email);
- varjson = eval(tt);//数组
- $.each(json, function (index, item) {
- //循环获取数据
- varname = json[index].Name;
- varidnumber = json[index].IdNumber;
- varsex = json[index].Sex;
- $("#list").html($("#list").html() +"<br>"+ name +" - "+ idnumber +" - "+ sex +"<br/>");
- });
- }
- });
- "list">
- <%@ 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";
- stringJsonStr = JsonConvert.SerializeObject(CreateDT());
- context.Response.Write(JsonStr);
- context.Response.End();
- }
- #region创建测试数据源//创建DataTable
- protected DataTable CreateDT()
- {
- DataTable tblDatas =newDataTable("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 =newDataColumn[] { 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;
- }
- }
- }
- "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": "[email protected]" };
- //json = eval(json);
- //alert("===json:id=" + json.id + ",uname=" + json.uname + ",email=" + json.email);
- varjson = eval(tt);//数组
- $.each(json, function (index, item) {
- //循环获取数据
- varKey = json[index].key;
- varInfo = json[index].info;
- // var idnumber = json[index].IdNumber;
- // var sex = json[index].Sex;$("#list").html($("#list").html() +"<br>"+ Key +"----"+ Info.name);//+ " - " + idnumber + " - " + sex + "<br/>");
- });
- }
- });
- -->
- <%@ 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();
- stringdata ="[{\"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"%>"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">"http://www.w3.org/1999/xhtml">"server">
- "JS/jquery-1.8.0.min.js"type="text/javascript">"text/javascript">
- function GetPara(o) {
- varsortid = $(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('');
- varjson = eval(tt);//数组
- $.each(json, function (index, item) {
- //循环获取数据
- varId = json[index].id;
- varName = json[index].name;
- $("#list").html($("#list").html() +"<br>"+ Name +"<input type='text' id='"+ Id +"' /><br/>");
- });
- }
- };
- function SavePara() {
- varparameter = {};
- $("#list input:text").each(function () {
- varkey = $(this).attr("id");
- varvalue = $(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) {
- }
- };
- "form1"runat="server">
- "ddl1"runat="server"onchange="GetPara(this)">"list">"button"value="保存数据"onclick="SavePara()"/>
- <%@ Page Language="C#"AutoEventWireup="true"CodeFile="Test2013.aspx.cs"Inherits="Test2013"%>"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">"http://www.w3.org/1999/xhtml">"server">
- "JS/jquery-1.8.0.min.js"type="text/javascript">"text/javascript">
- function GetPara(o) {
- varsortid = $(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('');
- varjson = eval(tt);//数组
- $.each(json, function (index, item) {
- //循环获取数据
- varId = json[index].id;
- varName = json[index].name;
- $("#list").html($("#list").html() +"<br>"+ Name +"<input type='text' id='"+ Id +"' /><br/>");
- });
- }
- };
- function SavePara() {
- varparameter = {};
- $("#list input:text").each(function () {
- varkey = $(this).attr("id");
- varvalue = $(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) {
- }
- };
- "form1"runat="server">
- "ddl1"runat="server"onchange="GetPara(this)">"list">"button"value="保存数据"onclick="SavePara()"/>
- <%@ 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";
- stringSortId = context.Request["sortid"];
- stringType = context.Request["type"];
- if(Type=="get")
- {
- if(!string.IsNullOrEmpty(SortId))
- {
- DataTable dt = MSCL.SqlHelper.GetDataTable("select * from PR_PRODUCTPARAS where sortid='"+ SortId +"' ");
- List list = newList();
- for(inti =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 =newSystem.IO.StreamReader(stream, System.Text.Encoding.GetEncoding("UTF-8"));
- stringsJson = sr.ReadToEnd();
- if(sJson.Contains("&"))
- {
- string[] sArr = sJson.Split('&');
- for(inti =0; i < sArr.Length; i++)
- {
- string[] sArr1 = sArr[i].Split('=');
- objectid = sArr1[0];
- objectvalue = sArr1[1];
- }
- }
- }
- else
- { }
- }
- public bool IsReusable {
- get {
- return false;
- }
- }
- public struct Paras
- {
- public string id;
- public string name;
- }
- }
Jquery 使用 Ajax 获取后台返回的 Json 数据后, 页面处理
来源: http://www.bubuko.com/infodetail-2156761.html