对于问题从后台获取json数据,将内容填充到下拉列表,代码非常简单,具体过程请看下面代码。
需求:url:链接 par:id sel:下拉列表选择器
//获取下拉列表
function buildselectbox(url, par, sel) { $(sel).empty(); $.getjson(url, { id: par }, function (json, textstatus) { for (var i = json.length - 1; i >= 0; i--) { $(sel).prepend('' + json[i].name + '') }; $(sel).prepend('请选择') });}
以上代码很简单吧,此问题很easy的解决了。
jquery 使用ajax获取后台返回的json数据页面处理过程
具体实现过程请看下面代码示例:
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; } } } 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; } } }
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 list = new list(); 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; } }
以上就是本文的全部内容,希望大家喜欢。