本文共 3212 字,大约阅读时间需要 10 分钟。
在使用Ajax实现Restful的时候,有时候会出现无法Put、Delete请求参数无法传递到程序中的尴尬情况,此时我们可以有两种解决方案:1、使用地址重写的方法传递参数。2、配置web.xml项目环境。
的程序为:
@RequestMapping(value = "/member", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8") public @ResponseBody Object edit(Member vo1) { log.info("【*** 修改用户信息 ***】" + vo1); JSONObject obj = new JSONObject(); obj.put("flag", true); return obj; } 1 2 3 4 5 6 7 1 2 3 4 5 6 7
一、使用地址重写的方法来实现put、delete请求的参数传递。
在js页面中(
$(editMember).on("click",function(){ $.ajax({ url : "member?empno=1009&ename=阿伦&sal=19777.77&hiredate=1969-10-10" , // 处理的请求路径 type : "put" , // 此处发送的是PUT请求(可变更为其他需要的请求) dataType : "json" , // 返回的数据类型为json类型 success : function(data) { $(showDiv).append("修改处理结果:" + data.flag + "
") ; } , error : function(data) { $(showDiv).append("对不起,出错啦!
") ; } }) ; }) ; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 8 9 10 11 12 13 14
二、使用配置文件修改来实现Put和Delete请求的参数传递
1、解决Put请求的参数传递,但是 无法解决 Delete 请求的传递
①、在项目中的web.xml文件中配置:
HttpMethodFilter org.springframework.web.filter.HttpPutFormContentFilter HttpMethodFilter /* 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
②在js文件中:
$(editBut).on("click",function(){ $.ajax({ url: "member", type : "put", // 此处发送的是PUT请求 data : { empno : 1170, ename : "SMITH", sal : 11.1, hiredate : "1991-11-11" }, success : function(data){ $(showDiv).append(" 数据更新成功:"+data.flag+"
"); console.log(1); }, dataType : "json", error : function(data){ $(showDiv).append("对不起,出错啦!
"); } }) }); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
2、解决 Put和Delete 请求的参数传递。
①、在项目中的web.xml文件中配置:
HiddenHttpMethodFilter org.springframework.web.filter.HiddenHttpMethodFilter HiddenHttpMethodFilter springmvc 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9
②在js文件中:
$(editBut).on("click",function(){ $.ajax({ url: "member", type : "post", // 此处发送的是POST请求 data : { _method : "put", // 将请求转变为PUT请求 empno : 1170, ename : "SMITH", sal : 11.1, hiredate : "11111-11-11" }, success : function(data){ $(showDiv).append(" 数据更新成功:"+data.flag+"
"); console.log(1); }, dataType : "json", error : function(data){ $(showDiv).append("对不起,出错啦!
"); } }) });
转载地址:http://qhwob.baihongyu.com/