注意: 返回数据类型用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 提交参数的方式总结
本文介绍了如何使用Ajax的POST方法提交多个参数到Spring MVC的Controller,并通过@RequestParam注解进行接收。示例代码包括POST提交的多个参数示例及jQuery AJAX GET提交单个参数的情况。
1691

被折叠的 条评论
为什么被折叠?



