zoukankan      html  css  js  c++  java
  • 使用jquery传递复杂Json数据——[ModelBinder(typeof(JsonBinder<复杂实体类>))]

    调用jQuery的ajax方法时,jQuery会根据post或者get协议对参数data进行序列化;

    如果提交的数据使用复杂的json数据,例如:

    {userId:32323,userName:{firstName:"李",lastName:"李大嘴"}}

    那么服务器是无法正常接收到完整的参数,因为jQuery对data的序列化,是使用了键值对拼装的方式;

    参数拼装成 userId=32323&userName=object ; userName所指向的对象被序列化成字符串"object"

            public ActionResult Add([ModelBinder(typeof(JsonBinder<CostItem>))]CostItem costItem)

    $(function() {
    /*按钮点击事件*/
    $("#btn_post_test").click(function() {
    var data = [
    { UserId: "11", UserName: { FirstName: "323", LastName: "2323" }, Keys: ["xiaoming", "xiaohong"] },
    { UserId: "22", UserName: { FirstName: "323", LastName: "2323" }, Keys: ["xiaoming", "xiaohong"] },
    { UserId: "33", UserName: { FirstName: "323", LastName: "2323" }, Keys: ["xiaoming", "xiaohong"] }
    ];
    $.post("Home/Test", { users: String.toSerialize(data) }, function(text) {
    alert(String.toSerialize(text));
    }, "json");
    });
    });

    public ActionResult Test([ModelBinder(typeof(JsonBinder<User>))]List<User> users)
    {
    return Json(users, JsonRequestBehavior.AllowGet);
    }
    }

    参考:https://www.cnblogs.com/0to9/p/5072345.html

    https://blog.csdn.net/LucasLi2016/article/details/53607828?locationNum=5&fps=1

    此随笔或为自己所写、或为转载于网络。仅用于个人收集及备忘。

  • 相关阅读:
    吹气球
    Leetcode 235
    什么是BPMN网关?
    BPMN中的任务和活动之间有什么区别?
    两款流程图设计推荐
    Activiti7.1, jBPM7.25, Camunda, Flowable6.3技术组成对比
    Flowable与activiti对比
    机器学习中的数学
    WopiServerTutorial
    如何整合Office Web Apps至自己开发的系统(二)
  • 原文地址:https://www.cnblogs.com/shy1766IT/p/13932896.html
Copyright © 2011-2022 走看看