springMVC中使用ajax传递json数组

解决方法

去www.json.org下载JSON2.js
再调用JSON.stringify(JSONData)将JSON对象转化为JSON串。

var people = [{ "UserName": "t1", "PassWord": "111111", "Sex": "男" }, { "UserName": "t2", "PassWord": "222222", "Sex": "女"}];

再构造URL回传给服务器端:
$("#btnSend").bind("click", function() {
$.post("a.ashx", {xxxx:JSON.stringify(people)}, function(data, returnstatus) { }, "json");
});

function customerCheck() {
                    selectRows = $("#ui_customerCheck_dg").datagrid('getSelections');
                    
                    var jsonData = JSON.stringify(selectRows);
                    console.info(jsonData);

                    if (selectRows.length > 0) {
                        console.info(selectRows);
                       
                        $.ajax({
                            url: 'ashx/bg_customerCheck.ashx?action=checkedPass',
                            data: { "selectRows": jsonData },
                            dataType: 'html',
                            success: function (rData) {
                                var dataJson = eval('(' + rData + ')');    //转成json格式
                                if (dataJson.success) {
                                    $.show_warning("提示", dataJson.msg);
                                    $("#ui_customerCheck_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                                } else {
                                    $.show_warning("提示", dataJson.msg);
                                }
                            }
                        });
                    }
                    else {
                        $.show_warning("提示", "请选择需要审核的单据!");
                        return;
                    }
                    //console.info(selectRows);
                    //$("#ui_customerCheck_dg").datagrid('unselectAll');

                }
using System;
using System.Collections.Generic;
using System.Web;
using LT.EPC.BLL;
using LT.EPC.Common;
using LT.EPC.Model;
using LT.EPC.SQLServerDAL;

namespace LT.EPC.WebUI.admin.ashx
{
    /// <summary>
    /// bg_customerCheck 的摘要说明
    /// </summary>
    public class bg_customerCheck : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            string action = context.Request.Params["action"];
            UserOperateLogDataContract userOperateLog = null;   //操作日志对象
            try
            {
                UserDataContract user = UserHelper.GetUser(context); //获取cookie里的用户对象
                userOperateLog = new UserOperateLogDataContract();
                userOperateLog.UserIp = CommonHelper.GetIP(context.Request.UserHostAddress);
                userOperateLog.UserName = user.UserId;

                switch (action)
                {
                    case "checkedPass":
                        var selectRowsJson = context.Request.Params["selectRows"] ?? "";

                        List<UserDataContract> userList = DeserializeUserList(selectRowsJson);

                        List<CustomerCheckDataContract> checkList = DeserializeCheckList(selectRowsJson);

                        if (new CustomerCheckMgr().ChangeOrderStatus(checkList))
                        {
                            userOperateLog.OperateInfo = "客服审核";
                            userOperateLog.IfSuccess = true;
                            userOperateLog.Description = "审核通过" + null;
                            context.Response.Write("{\"msg\":\"审核通过!\",\"success\":true}");
                        }
                        else
                        {
                            userOperateLog.OperateInfo = "客服审核";
                            userOperateLog.IfSuccess = false;
                            userOperateLog.Description = "客服审核失败";
                            context.Response.Write("{\"msg\":\"客服审核失败!\",\"success\":false}");
                        }

                        UserOperateLogMgr.InsertOperateInfo(userOperateLog);
                        break;

                    default:
                        context.Response.Write("{\"result\":\"参数错误!\",\"success\":false}");
                        break;
                }
            }
            catch (Exception ex)
            {
                context.Response.Write("{\"msg\":\"" + JsonHelper.StringFilter(ex.Message) + "\",\"success\":false}");
                userOperateLog.OperateInfo = "客服审核功能异常";
                userOperateLog.IfSuccess = false;
                userOperateLog.Description = JsonHelper.StringFilter(ex.Message);
                UserOperateLogMgr.InsertOperateInfo(userOperateLog);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        //直接将Json转化为实体对象
        public List<UserDataContract> DeserializeUserList(string json)
        {
            var u = JsonHelper.FromJson<List<UserDataContract>>(json);
            return u;
        }

        //直接将Json转化为实体对象
        public List<CustomerCheckDataContract> DeserializeCheckList(string json)
        {
            var o = JsonHelper.FromJson<List<CustomerCheckDataContract>>(json);
            return o;
        }
    }
}
posted @ 2016-05-11 22:25  goumingming  阅读(552)  评论(0编辑  收藏  举报