通过 Ajax 发送 PUT、DELETE 请求的两种实现方式

     通过 ajax 发送 PUT、DELETE 请求有以下两种方式:

一、普通请求方法发送 PUT 请求

     1. 如果不用 ajax 发送 PUT,我们可以通过设置一个隐藏域设置 _method 的值,如下:

	<form action="/emps" method="post">
		<input type="hidden" name="_method" value="PUT">
		<input type="text" name="username">
		<input type="password" name="password">
        <input type="submit"/>
	</form>

      2. 控制层:

    @RequestMapping(value="/emps", method=RequestMethod.PUT)
	public String updateEmp(Employee employee) {
		System.out.println(employee);
		return "redirect:success.jsp";
	}

       注意:这里如果采用请求转发的方式即 return "success.jsp",将会出现以下问题:(Tomcat 7.0 版本不会出现这个问题)
                 SpringMVC HTTP Status 405 - JSPs only permit GET POST or HEAD
       采用重定向的方式可以避免这个问题。


       3. 这种方式发送 PUT 请求,还需在 web.xml 中配置一个 HiddenHttpMethodFilter 过滤器
 

	<filter>
		<filter-name>HiddenHttpMethodFilter</filter-name>
		<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>HiddenHttpMethodFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

 原理/问题:为什么要设置 _method 参数?
        浏览器 form 表单只支持 GET 与 POST 请求,而DELETE、PUT 等 method 并不支持,Spring3.0 添加了一个过滤器,可以将这些请求转换为标准的 http 方法,使得支持 GET、POST、PUT 与 DELETE 请求。
         首先看到 HiddenHttpMethodFilter 过滤器的源码:

        doFilterInternal方法能根据 _method 把 post 请求转化为标准的Http方法,即GET,、POST、 HEAD、OPTIONS、PUT、DELETE、TRACE

 

二、通过 ajax 发送 DELETE 请求(需要带_method参数)

       这种实现方式跟第一种的原理是一样的,同样是利用 _method 参数对 request 请求进行转换,所以这种方式同样需要配置HiddenHttpMethodFilter 过滤器。

       以下以删除员工为例子:

        /* 删除员工 */
		function deleteEmp(empId){
			$.ajax({
				url : "emps",
				data : {_method : "DELETE", empId : empId},
				type : "POST",
				success : function(result){
				
				}
			})
		}

        发送表单 ajax 请求:

$("#updateBtn").click(function(){
			$.ajax({
				url : "emps",
				data : $("#updateEmpFormNode").serialize()+"&_method=put",
				type : "post",
				success : function(result){
					alert(result);
				}
			})
			
		})

 

三、直接指定 ajax 请求中的 type 为 put/delete(不带 _method 参数)

         1. 把上述第二点的表单更新改写为如下:

$("#updateBtn").click(function(){
			$.ajax({
				url : "emps",
				data : $("#updateEmpFormNode").serialize(),
				type : "PUT",
				success : function(result){
					alert(result);
				}
			})
			
		})

        出错:

       原因:
       这问题是由于 Tomcat 本身引起的,导致这个问题是因为 SpringMVC 绑定 POJO 对象时获取不到数据,然后执行更新语句时 sql 语句出错导致的。由于 POJO 的数据都为空,所以被执行的更新语句可能会为 update emp set where empId = ?,反正被执行更新语句肯定是有错的。想要知道为什么获取不到数据,下面首先先了解一下 Tomcat 如何封装数据以及SpringMVC如何绑定数据

       1.1 Tomcat 封装表单数据和 SpringMVC 绑定 POJO 对象数据时的流程如下:
                ① Tomcat 首先会将请求体中的数据,封装一个map。
                ② request.getParameter("empName") 就会从这个map中取值。
                ③ SpringMVC封装POJO对象的时候,通过 request.getParamter("empName"); 获取一个字段的值,然后赋值到      POJO 中属性名为 empName 的属性。如:
                           String temp = (String)request.getParamter("empName");
                           Employee emp = new Employee();
                           emp.setEmpName(temp);

        1.2 由于 Ajax 发送的是 PUT 请求,Tomcat一看是PUT不会封装请求体中的数据为map,只有POST形式的请求才封装请求体为map,查看 Tomcat 的源码:
            查找到 protected void parseParameters() 该方法
            protected String parseBodyMethods = "POST";
            if( !getConnector().isParseBodyMethod(getMethod()) ) {
                      success = true;
                      return;
            }
           当 Tomcat 知道是请求不是 POST 请求时,会直接 return,而不会继续往下执行解析封装参数,所以当                        request.getParamter("empName") 从 map 取数据时,由于 empName 参数没有被封装到 map 中,getParmater获取到值为 null。

        2. 解决方法
             2.1 在 web.xml 配置上HttpPutFormContentFilter;

	<filter>
		<filter-name>HttpPutFormContentFilter</filter-name>
		<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>HttpPutFormContentFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

            2.2  HttpPutFormContentFilter 的作用;将请求体中的数据解析包装成一个map。

            2.3  request被重新包装,request.getParameter()被重写,就会从自己封装的map中取数据

 

 

 

 

 

 

 

  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值