gpt4 book ai didi

javascript - 405(方法不允许)删除

转载 作者:行者123 更新时间:2023-12-03 12:36:04 25 4
gpt4 key购买 nike

问题是这样的:Spring RESTful Web 服务和客户端。在服务器上执行 DELETE 请求时我得到 -> 删除http://localhost:8080/employee/3/logout 405(不允许的方法)

我已经实现了 CORS 过滤器,但它仍然不起作用。

员工 Controller

@Controller
@RequestMapping(value = "/employee")
public class EmployeeController {
private static final EmployeeRepository employeeRepository = new EmployeeRepository();
public EmployeeController(){
}

@RequestMapping(method = RequestMethod.GET, value = "/{employeeId}")
public ResponseEntity<EmployeeDTO> showEmployeeById(@PathVariable int employeeId)
{
employeeRepository.setEmployeeAsActive(employeeId);
EmployeeDTO employeeDTO = employeeRepository.getEmployeeDTOForId(employeeId);
if(employeeDTO != null)
{
return new ResponseEntity<EmployeeDTO>(employeeDTO,HttpStatus.OK);
}
return new ResponseEntity<EmployeeDTO>(employeeDTO,HttpStatus.NOT_FOUND);
}

@RequestMapping(method = RequestMethod.GET, value = "/{employeeId}/viewTasks")
public ResponseEntity<List<TaskDTO>> showTasksForEmployeeId(@PathVariable int employeeId)
{
List<TaskDTO> taskDTOs = employeeRepository.getTasksForEmployee(employeeId);
if(taskDTOs != null)
{
return new ResponseEntity<List<TaskDTO>>(taskDTOs, HttpStatus.OK);
}
return new ResponseEntity<List<TaskDTO>>(taskDTOs, HttpStatus.NOT_FOUND);
}

@RequestMapping(method = RequestMethod.DELETE, value = "/{employeeId}/logout}")
public ResponseEntity<Void> logoutEmployeeById(@PathVariable int employeeId)
{
employeeRepository.logoutEmployeeById(employeeId);
return new ResponseEntity<Void>(HttpStatus.OK);
}

}

我提出请求的地方:

var url = "http://localhost:8080/employee/" + document.cookie1;
$('#employeeLogout').click(function(){
$.ajax({
type: "DELETE",
url: url + "/logout",
async: true
});
document.cookie1 = null;
window.location.assign('http://localhost:9000/#/employees');
});

Aa 和我的 CORS 过滤器

    @Component
public class CORSFilter implements Filter {
//Web container will call a filter when the request was made.
// CORS filter allows Cross-Origin requests-responses to be performed by adding Access-Controll-Allow-Origin in the header"
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
chain.doFilter(req, res);
}

public void init(FilterConfig filterConfig) {}

public void destroy() {}
}

最佳答案

我也遇到过同样的问题,但仍然很难解决。问题出在 Spring Security 的某个地方,因为我的代码在没有安全性的情况下工作正常。但是当启用spring security时,只允许GET,PUT、POST和DELETE不起作用。

关于javascript - 405(方法不允许)删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23732437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com