前几天 用Spring MVC 开发一个demo 但是在前台想难道后台的返回值,始终拿不到 后来问了一个大神 直接就给我解决了 先把代码贴出来看一下
前台代码
$(function() {
$("#submit").click(function(){
$.ajax({
type:"POST",
url:path+"/rules/add",
data:{valid_begin_date:$("#valid_begin_date").val(),valid_end_date:$("#valid_end_date").val(),
visit_count:$("#visit_count").val(),valid_date:$("#valid_date").val(),comment: $("#comment").val(),
data_type:$("#data_type").val()
},
dataType: 'json',
success: function(result){
if(result == '1'){
alert("添加热数据规则成功")
location.href='/product/list';
}else{
alert("添加热数据规则失败");
}
}
})
});
})
后台代码
@RequestMapping(value = "add", method = RequestMethod.POST)
public String ruleAdd(@RequestParam(value = "valid_begin_date",required = true) String validBeginDate,
@RequestParam(value = "valid_end_date",required = true) String validEndDate,
@RequestParam(value = "visit_count",required = true) int visitCount,
@RequestParam(value = "valid_date",required = true) String validDate,
@RequestParam(value = "comment",required = false) String comment,
@RequestParam(value = "data_type",required = false) String dataType
) {
int result=ruleService.ruleAdd(validBeginDate, validEndDate, visitCount, validDate, comment, dataType);
return String.valueOf(result);
}
需要在后台代码添加注解 @ResponseBody
说一下@ResponseBody 我理解的意思 就是给ajax使用的,记住这点就行,比如增删改什么的,你需要返回个状态弹窗,告诉用户添加修改成功或者失败,就用这个