最近項目組有人問我,“springboot怎么接受不到前端提交的PUT和DELETE請求?”
於是有了這篇文章,本篇文章主要解決兩個問題:
- js好像只能提交GET、POST請求耶?怎么提交PUT DELETE請求?
- 如何將前端提交的PUT、DELETE請求與server端對接上?
問題1解決方案:
在ajax中發送POST請求,帶上_method參數,_method值為PUT或者DELETE
實例:
$.ajax({
url:"",
type:"POST",
data:{
_method:"PUT"
},
success:function(data){...}
})
問題2解決方案:
配置HiddenHttpMethodFilter
實例:
@Configuration
public class HttpRequestConfig {
@Bean
public HiddenHttpMethodFilter hiddenHttpMethodFilter() {
HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
hiddenHttpMethodFilter.setBeanName("HiddenHttpMethodFilter");
hiddenHttpMethodFilter.setMethodParam("_method");
return hiddenHttpMethodFilter;
}
}