后台@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/ (点击进入)

网站特点:

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

看一小点点的截图:

或可一试哦!

 

  • 7
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值