How do web projects support DELETE and PUT requests

Transfer from liuyuanjiang109 : https://blog.csdn.net/liuyuanjiang109/article/details/78972644
Ajax uses the restful service to send put and delete requests directly when there is a problem with the participation

1. When using the POST + _method:delete/put + filter method,
Ajax needs to pass parameters when sending put and delete requests. If the parameters are on the URL address bar, they can be used normally.

If you need to pass parameters in data: (the browser will use the form submission method to submit), you need to pay attention to the following changes:

 1. The request method is set to type: "post",

2. Add __method:"DELETE", or _method:"PUT" parameter to data,

data:{_method:"DELETE", id:issueId,userId:userId},
3. The controller in the background is still the corresponding DELETE request

@RequestMapping(value="/answer/{answerId}",method=RequestMethod.DELETE)
public ResponseResult deleteAnswer(@PathVariable("answerId")int answerId,Issue issue){     //When it can be automatically encapsulated into an object, it can be used directly Object parameter } 4. The corresponding filter needs to be configured (it will be configured automatically if Spring Boot is used)


<filter>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <!-- 备注,这边的名称必须和配置'springmvc'的servlet名称一样 -->
    <servlet-name>springmvc</servlet-name>    
</filter-mapping>  


The ajax code is as follows:
 

var r=confirm("确认删除该?");
if(r){
  $.ajax({
    url:"http://localhost:8888/answer/"+answerId,
    type:"POST",
    data:{_method:"DELETE", id:issueId,userId:userId},
    dataType:"json",
    success:function(result){
        }
                            
   },
            
 });

PS: If you use springboot, you can omit step 4, because when springboot starts, HiddenMethodFilter
two will be loaded , and PUT DELETE request
is still used. 1. Put and delete requests are still used, and data needs to be set to json string when parameters need to be passed.

var jsonstr = {"id":issueId,"userId":userId};
var r=confirm("确认删除该回答?");
if(r){
   $.ajax({
    url:"http://localhost:8885/answer/"+answerId,
    type:"DELETE",
    contentType:"application/json",//设置请求参数类型为json字符串
    data:JSON.stringify(jsonstr),//将json对象转换成json字符串发送
    dataType:"json",
    success:function(result){
    
        },
            
   });
}


The client needs to use @RequestBody annotation

@RequestMapping(value="/answer/{answerId}",method=RequestMethod.DELETE)
public ResponseResult deleteAnswer(@PathVariable("answerId")int answerId,@RequestBody Issue issue){
    
}


Finally, if the front-end reports an error prompting Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response, you can refer to the following solutions

https://stackoverflow.com/questions/12409600/error-request-header-field-content-type-is-not-allowed-by-access-control-allow  

Can write a filter

@WebFilter(servletNames={"dispatcherServlet"})//可配置对应的请求servlet 此处使用 springMVC 
public class AjaxFilter implements Filter{
    
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletResponse httpServletResponse=(HttpServletResponse) response;
        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
        httpServletResponse.setHeader("Access-Control-Allow-Methods","GET,POST,DELETE,PUT");
        httpServletResponse.setHeader("Access-Control-Allow-Headers","Origin,X-Requested-With,Content-Type,Accept");              
        chain.doFilter(request, response);      
    }
 
   
 
}


 

Guess you like

Origin blog.csdn.net/qq_28411869/article/details/87866584