2017-08-14 74 views
0

我在我的ajax中写了一个请求(method:delete)。我在我的控制器中使用了一个deleteMapping。当我触发这个请求时,我得到了一个400错误。但在浏览器中我可以看到数据专案编号enter image description hereSpring controller必需字符串参数不存在

控制台说Required String parameter 'projectId' is not present

总日志

2017-08-14 13:06:11.296 WARN 8584 --- [nio-8080-exec-3] o.s.web.servlet.PageNotFound    : Request method 'DELETE' not supported 
2017-08-14 13:06:11.296 WARN 8584 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'DELETE' not supported 

2017-08-14 12:32:57.239 WARN 8584 --- [nio-8080-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'projectId' is not present 

这里是AJAX

$('#delete-project-btn').on('click', function() { 
    if (cur != null) { 
     alert(cur); 
     if(window.confirm('sure?')) { 
      $.ajax({ 
       url : '/index/'+cur, 
       type : 'delete', 
       dataType : 'text', 
       data : { 
        projectId : cur 
       },    
      }); 
     } 
    } 

}) 

和我的controller

 @DeleteMapping("/index/{projectId}") 
    public String deleteProject(@PathVariable("projectId") int id) { 
     System.out.println(id); 
//  projectRepository.delete(Integer.valueOf(id)); 
     return "redirect:/index"; 
    } 

问题在哪里?

+2

在你的Ajax请求,你应该通过专案编号为的一部分请求参数不请求正文。 – Barath

回答

4

按照下面的评论链接,

如果DELETE请求包括实体主体,身体被忽略

$('#delete-project-btn').on('click', function() { 
    if (cur != null) { 
     alert(cur); 
     if(window.confirm('sure?')) { 
      $.ajax({ 
       url : '/index?projectId='+cur, 
       type : 'delete', 
       dataType : 'text' 

      }); 
     } 
    } 

}) 
+0

阅读完整的讨论在这里https://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request – Barath

+0

如何更改我的控制器,以获得该ID? –

+0

我认为你的控制器是正确的,只是尝试更改上述更新的AJAX请求url – Barath

相关问题