MVC中使用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;
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM