步骤

  • 配置HiddenHttpMethodFilter (spring3.0提供的过滤器,可将delete、put转为标准的Http请求)
  • 写ajax

配置HiddenHttpMethodFilter

springboot配置(2以上版本需要在配置文件开启)

//application.properties中配置
#开启hiddenHttpMethodFilter处理DELETE、PUT请求 springboot默认不开启false
spring.mvc.hiddenmethod.filter.enabled=true

web工程配置

//在web.xml中配置
<filter>
	<filter-name>hiddenHttpMethodFilter</filter-name>
	<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
	<filter-name>hiddenHttpMethodFilter</filter-name>
	<servlet-name>/*此处写Servlet映射名称*/</servlet-name>
</filter-mapping>

Ajax

1、发送参数写在url中的情况

若参数直接写在AJax的url属性中,例:url:“admin/course?pagesize=5”
则发送delete(put也一样)

$.ajax({
	url: "/admin/course?pagesize=5",
	type: "DELETE",
	contentType: "application/json;charset=UTF-8",
	...
	success: function(data){
		//成功处理函数体
	}
});

2、发送参数写在data中的情况

①请将提交请求类型type设置为post
②请在data参数中加入“ _method:‘DELETE’ ”

$.ajax({
	url: "/admin/course",
	type: "POST",
	//这里的contentType不要写成json 直接删除 
	//使用默认的application/x-www-form-urlencoded; charset=UTF-8 数据格式
	contentType: "application/json;charset=UTF-8", 
	data: {_method:"DELETE",...},
	...
	success: function(data){
		//成功处理函数体
	}
});

ps:另一种写法(注意put不需要加单引号)

更多推荐

使用Ajax发送DELETE、PUT请求方法