【问题标题】:Post two array as json through ajax to Spring Controller通过ajax将两个数组作为json发布到Spring Controller
【发布时间】:2015-01-08 07:06:16
【问题描述】:

我的ajax方法

$.ajax( {

            type: "POST",
            contentType: 'application/json;charset=utf-8',
            dataType:'json',
            url: 'addrequisition',
            data: JSON.stringify([{ ids: val, qty: valtxt }]),
            success: function(result)
            {
                $("#result").html(result);
            }
        });
    });

我的数组是 val 和 valtxt。 我想在 Spring Controller 中读取这些数组帮助我:)

【问题讨论】:

    标签: ajax json spring


    【解决方案1】:

    首先,你需要像这样在java中定义一个类:

    class MyClass{
    private String ids;
    private String qty;
    //Setters and Getters
    
     }
    

    请注意,类的成员必须与 json 数据相同。 然后在你的控制器中你需要定义这样的动作:

    @RequestMapping(value = "/addrequisition", method = RequestMethod.POST)
    public String addrequisition(@RequestBody MyClass myClass) {
    
        String result = myClass.getIds() + myClass.getQty();
        return result;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-07-25
      • 2013-09-04
      • 1970-01-01
      • 1970-01-01
      • 2016-11-06
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      • 2023-03-17
      相关资源
      最近更新 更多