有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

ajax请求时请求方法的javascript奇怪行为

我的控制器中有这样的方法

@Controller
@RequestMapping ("/admin/users")
public class AdminUserController {
..
  @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  public @ResponseBody boolean deleteUser(@PathVariable("id") int id,
                             HttpServletResponse response) {
     ..
  }
..
}

这是ajax请求

$.ajax({
  url: '/admin/users/'+id,
  type: 'delete',
  success: function(data){
    console.log(data);
  },
  error: function(e){
    console.log(e);
  }
});

当我发送这个请求时,它失败了,我得到405。当我查看响应头时,我看到了这个Allow:"GET"

嗯。我将ajax请求中的“delete”更改为“get”,但随后我得到了响应Allow:"DELETE"

可能是什么


共 (1) 个答案

  1. # 1 楼答案

    我认为您的服务器安全配置不允许删除操作,基本上是头部:

    ALLOW: GET
    

    建议您尝试GET请求,但由于您指定

    method = RequestMethod.DELETE
    

    Spring拒绝了对该方法的GET调用

    你应该把method = RequestMethod.DELETE改成method = RequestMethod.GET 并发出HTTP GET请求

    如果有帮助,请告诉我