后台@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);
    }

 

 

======以下于你或许是个好消息======

 

好消息就是:欢迎访问我的个人博客网站哈哈哈......


网站名称:Java学习笔记网 (点击进入)

url:https://www.javaxxbj.com/personalHomePage.html?id=null&userId=1578999242684 (点击进入)

网站特点:

  1. 你可以记录自己的博客,并可以控制显示和隐藏,可利于管理啦!!!
  2. 可以添加收藏各个网站的链接!!!
  3. 甚至也可以文章收藏,点赞,关注,查看我的消息等功能哦!!1

看一小点点的截图:

欢迎使用!!!欢迎使用!!!欢迎使用!!!欢迎使用!!!欢迎使用!!!欢迎使用!!!欢迎使用!!!

 

你可能感兴趣的:(ajax)