ajax post提交多个参数后台controller @RequestParam方式接收

本文介绍了如何使用Ajax的POST方法提交多个参数到Spring MVC的Controller,并通过@RequestParam注解进行接收。示例代码包括POST提交的多个参数示例及jQuery AJAX GET提交单个参数的情况。

注意: 返回数据类型用dataType: ‘text’,如果是 dataType: ‘json’,success获取不到

####1.ajax post提交多个参数后台controller @RequestParam方式接收

<script >
    function goRead( id, cartoonId){
        alert("id="+id+"  cartoonId="+cartoonId)
        var params = {
            "page":0,
            "size":3,
            "cartonDetailNum":id,
            "cartoonId":cartoonId
        }
        $.ajax({
                type: "post",
                url: "/chapterImage/findPage",
                data: params,
                // data: {"page":"0","size":"3","cartonDetailNum":id,"cartoonId":cartoonId},//或者这样写进去
                dataType: 'json',
                success: function (r) {
                    alert("请求成功"+r);
                    window.location.reload();

                },
            error:function(result) {
                alert("error");
            }
            })
    } ;
   </script>

@RequestMapping(value = “/findPage”,method = RequestMethod.POST)
public String doFindPageByPageNum(@RequestParam(value = “page”) String page, @RequestParam String size,@RequestParam String cartonDetailNum,String cartoonId){
//代码省略
}

这里写代码片

####例子2:

<script >
    $(function () {
        var content = $('#all');
        var params = {
            "pageIndex": 1,
            "pageSize": 3,
            "categoryId": 2,
        }
        $.ajax({
            type: "post",
            url: "/manager/findVideoByCategoryId",
            data: params, 
            dataType: 'json',
            success: function (r) {
                alert("请求成功" + r);
                $("#content").load(r) 

            },
            error: function (result) {
                alert("error");
            }
        })
    });
</script>

controller接收:

 @RequestMapping(value = "/findVideoByCategoryId",method = RequestMethod.POST)
    @ResponseBody
    public String findVideoByCategoryId( String pageIndex,
                                         String pageSize,  String categoryId,HttpServletRequest request,HttpServletResponse response){
                                         //logger.info("----ajax POST 方式提交后台接收-----");
                                         //HttpServletRequest,HttpServletResponse只是用于验证登录token
     }

####2. jquery ajax get方式提交单个参数后台controller @RequestParam方式接收

<a href="javascript:void(0)" onclick= "javascript:goRecharge()"  target="_blank" >确定</a>

<script>
function goRecharge(){ 
        var params =$("input[name='optionsRadiosinline']:checked").val(); //获取单选的值
        $.ajax({
            type: "GET",
            url: "/doWxPayTest",
            data: "totalFee="+params,
            dataType: 'text',
           contentType: 'application/json;charset=UTF-8',
            success: function (r) { 
                if (r.trim() == "y") { //如果返回值为y,则跳转处理
                    window.location.href = "/wxPay?totalFee="+$("input[name='optionsRadiosinline']:checked").val();
                } else {
                    alert("请求成功,但返回值错误")
                }
            }
        })
    }
    </script>

controller:

@RequestMapping(value = "/doWxPayTest",method = RequestMethod.GET)
    @ResponseBody
    public String dowxPayTest(Model model,@RequestParam String totalFee){ 
        if(!totalFee.equals("")&&totalFee!=null) {
            total_fee = Integer.parseInt(totalFee);
        }
        logger.info("wxPayTest total_fee="+total_fee);
        return "y";
    }

最后附上,忘记时查看
后台接收POST GET 提交参数的方式总结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值