注意: 返回数据类型用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)@ResponseBodypublic 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)@ResponseBodypublic 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 提交参数的方式总结
如果觉得《ajax post提交多个参数后台controller @RequestParam方式接收》对你有帮助,请点赞、收藏,并留下你的观点哦!