预检响应具有无效的 HTTP 状态代码 405 - AngularJS + Spring MVC

Response for preflight has invalid HTTP status code 405 - AngularJS + Spring MVC

本文关键字:代码 AngularJS MVC Spring 状态 HTTP 响应 无效      更新时间:2023-09-26

我试图在这里发出 Post 请求,但没有成功。

Angularjs code:

function Login(username, password, callback) {
      $http.post('http://localhost:8080/basic-web-app/login', { username: username, password: password })
            .success(function (response) {
                console.log(response);
                       callback(response);
            });
    }

服务器代码:

@RequestMapping(value =  "/login", method = RequestMethod.GET)
public String login(@RequestBody Login login) {
    if (login.username.equals("test") && login.password.equals("test")) {
        return "success";
    } else {
        return "not success";
    }
}

错误: XMLHttpRequest 无法装入http://localhost:8080/basic-web-app/login 。预检响应具有无效的 HTTP 状态代码 405

问题是你正在制作一个帖子

 $http.post(

Spring MVC 预计会出现 GET

@RequestMapping(value =  "/login", method = RequestMethod.GET)

我建议将您的控制器定义更改为 POST