使用JQuery 向后台传值
1. 通过地址传值
可以通过 window.location.href=“地址” 传值到后台
$("update").on("click",function(){
window.location.href = "<%request.getContPath()%>controller/update?id="+$(this).attr("id");
})
2. 通过 ajax 传值到后台
$("delete").on("click",function(){
var flag = confirm("确认删除吗?");
if(flag){
$.ajax({
url:"user/delete", //向后台传的路径
type:"post",
data:{id:id}, //向 后台传的 值
dataType:"JSON",
success:function(d){ // d是后台传回来的数据内容
if(d.code == "200"){
alert(msg);
window.location.onload(); //删除后页面重新刷新一次
}
},
error:function(){
alert(msg + ",如果您多次雨打此信息,请联系管理员");
}
});
}
})
通过 ajax 提交时 , 在后台的 controller 的方法中,要加上 @ResponseBody 注解
@RequestMapping("delete")
@ResponseBody
public Map<String,String> delete(Long id){
Map<String,String> map=new HashMap<>();
System.out.println(id);
int i = userMapper.deleteByPrimaryKey(id);
if(i>0) {
map.put("code", "200");
map.put("msg", "删除成功");
}else{
map.put("code", "500");
map.put("msg", "删除失败");
}
return map;
}