后台 @RequestParam 接收参数,js 中 ajax 怎么传参

注意:传的参数类型必须和后台定义的参数类型一致,否则会报 400 错误。
type=post : post 提交
type=get: get 提交
get 和 post 提交的区别:get 请求的参数会和 url 拼接起来,当参数较多时,会导致 url 过长。
所以:当参数较多时,适合 post 提交;参数少时适合 get 提交,但 get 提交会引起汉字乱码;
注意前后台的 type 一致。
(也可以直接将参数以?& 的方式拼接在 url 后面,这种方式是 get 提交)

一、get 提交

js:

contentType : 'application/x-www-form-urlencoded'
或者 contentType : 'application/json' 都可
但 data 里面必须是 json 字符串,即 var jsonStr = {"pageNum":2, "pageSize":20} 格式;不能用 JSON.stringify(jsonStr) 来转化;否则后端接收到的数据为 null

var jsonStr = {"pageNum":2, "pageSize":20, "serialNumber":serialNumber, "name":name, 
   "capacity":capacity, "createTimeStart":createTimeStart, "createTimeEnd":createTimeEnd};
            var url = "commodityTemplate/list";
            $.ajax({
                async : false,
                url : url,
                type : 'get',
                contentType : 'application/x-www-form-urlencoded',
                //或者contentType : 'application/json',
                dataType:'json',
                data : jsonStr,
                success : function(o) {
                    callback(o);
                },
                error:function(){
                    alert("出错啦...");
                },
            });

controller:

get 提交时,后端会有乱码,此时需要进行编码转换
String name = new String(name.getBytes(“ISO-8859-1”), “UTF-8”)

 @RequestMapping(value ="/list", method = RequestMethod.POST)
    @ResponseBody
    public ResultObject list(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                             @RequestParam(value = "serialNumber", required = false)String serialNumber,
                             @RequestParam(value = "name", required = false)String name,
                             @RequestParam(value = "capacity", required = false)Integer capacity,
                             @RequestParam(value = "createTimeStart", required = false)Long createTimeStart,
                             @RequestParam(value = "createTimeEnd", required = false)Long createTimeEnd) throws Exception{
 
        //封装查询条件
        Map map = new HashMap();
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        map.put("serialNumber", new String(serialNumber.getBytes(“ISO-8859-1”), “UTF-8”));
        map.put("name", new String(name.getBytes(“ISO-8859-1”), “UTF-8”));
        map.put("capacity", capacity);
        map.put("createTimeStart", createTimeStart);
        map.put("createTimeEnd", createTimeEnd);
 
        return commodityTemplateService.list(map);
    }

二、post 提交

js:

注意:
1、contentType : 'application/x-www-form-urlencoded'
不能是:contentType : 'application/json',否则后端接收到的数据为 null
2、data 里面必须是 json 字符串,即 var jsonStr = {"pageNum":2, "pageSize":20} 格式;不能用 JSON.stringify(jsonStr) 来转化;否则后端接收到的数据也为 null

var jsonStr = {"pageNum":2, "pageSize":20, "serialNumber":serialNumber, "name":name, 
   "capacity":capacity, "createTimeStart":createTimeStart, "createTimeEnd":createTimeEnd};
            var url = "commodityTemplate/list";
            $.ajax({
                async : false,
                url : url,
                type : 'POST',
                contentType : 'application/x-www-form-urlencoded',
                dataType:'json',
                data : jsonStr,
                success : function(o) {
                    callback(o);
                },
                error:function(){
                    alert("出错啦...");
                },
            });

controller:

post 提交,后端接收的参数不会出现乱码,因为在 web.xml 中已经处理了 post 请求的乱码。

 @RequestMapping(value ="/list", method = RequestMethod.POST)
    @ResponseBody
    public ResultObject list(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                             @RequestParam(value = "serialNumber", required = false)String serialNumber,
                             @RequestParam(value = "name", required = false)String name,
                             @RequestParam(value = "capacity", required = false)Integer capacity,
                             @RequestParam(value = "createTimeStart", required = false)Long createTimeStart,
                             @RequestParam(value = "createTimeEnd", required = false)Long createTimeEnd) throws Exception{
 
        //封装查询条件
        Map map = new HashMap();
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        map.put("serialNumber", serialNumber);
        map.put("name", name);
        map.put("capacity", capacity);
        map.put("createTimeStart", createTimeStart);
        map.put("createTimeEnd", createTimeEnd);
 
        return commodityTemplateService.list(map);
    }