How does the springboot restful interface accept put and delete requests from the front end

Time:2021-10-24

Recently, someone in the project team asked me, “why can’t springboot accept put and delete requests submitted by the front end?”
Therefore, with this article, this article mainly solves two problems:

  • JS can only submit get and post requests? How to submit a put delete request?
  • How to connect put and delete requests submitted by the front end to the server end?

Problem 1 solution:
Send a post request in Ajax with_ Method parameter_ The method value is put or delete
example:

$.ajax({
    url:"",
    type:"POST",
    data:{
      _method:"PUT"
      },
    success:function(data){...}
 })

Problem 2 solution:
Configure hiddenhttpmethodfilter
example:

@Configuration
public class HttpRequestConfig {
    @Bean
    public HiddenHttpMethodFilter hiddenHttpMethodFilter() {
        HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
        hiddenHttpMethodFilter.setBeanName("HiddenHttpMethodFilter");
        hiddenHttpMethodFilter.setMethodParam("_method");
        return hiddenHttpMethodFilter;
    }
}

Recommended Today

SQL exercise 20 – Modeling & Reporting

This blog is used to review and sort out the common topic modeling architecture, analysis oriented architecture and integration topic reports in data warehouse. I have uploaded these reports to GitHub. If you are interested, you can have a lookAddress:https://github.com/nino-laiqiu/TiTanI recorded a relatively complete development process in my hexo blog deployed on GitHub. You can […]