问题:在一般处理程序(.ashx)中累加[index=‘b‘]的值 难点:前台获取JSON值后台解析 解决: //#region 把index=b的值存在JSON对象中 function AjaxGetSum() { var arr = []; $("input[index=‘b‘]").each(function () { var arrObject = {}; var tempVal = $(this).val(); if (!$.gstr.isEmpty(tempVal)) { //本公司自己封装的js $.gstr.isEmpty(st) 判断st是否为空,返回true/false arrObject["inValue"] = tempVal; arr.push(arrObject); } }); var JsonString = JSON.stringify(arr); //转换成JSON类型的字符串 $.post("HandlerSum.ashx", { jsonVar: JsonString }, function (data) { alert(data); }) } //#endregion
using System; using System.Collections.Generic; using System.Linq; using System.Web; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Converters; namespace Train_First.Ajax_Rpc { /// <summary> /// HandlerSum 的摘要说明 /// </summary> public class HandlerSum : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string ss = context.Request.Form["jsonVar"]; //获取前台传递过来的授课JSON字符串数组 JArray javascript = (JArray)JsonConvert.DeserializeObject(ss); //反序列化获取的JSON字符串数组 string StringSum = ""; for (int i = 0; i < javascript.Count; i++) { JObject obj = (JObject)javascript[i]; string outValue = obj["inValue"].ToString(); //将一个个反序列化的JSON字符串数组转换成对象 StringSum += outValue; } context.Response.Write(StringSum); } public bool IsReusable { get { return false; } } } }
时间: 2024-11-06 03:55:39