简体   繁体   English

为什么我的浏览器在应该发送 POST 请求的地方发送 GET 请求 [Spring-Boot]?

[英]Why does my browser send a GET request on a form where it should be sending a POST request[Spring-Boot]?

I am working on an application using Spring Boot MVC and I have a login page and whenever I input data on the forms using chrome my browser doesn't redirect me to the page I've specified in my Controler class but instead It sends a GET request where it should be sending a POST request.我正在使用 Spring Boot MVC 开发一个应用程序,我有一个登录页面,每当我使用 chrome 在 forms 上输入数据时,我的浏览器不会将我重定向到我在控制器 class 中指定的页面,而是发送一个 GET request 它应该发送 POST 请求的地方。 This is my controller class这是我的 controller class

@Controller
@RequestMapping("/login")
public class loginController {
private final AuthService authService;

public loginController(AuthService authService) {
    this.authService = authService;
}

@GetMapping
public String returnLogIn() {
    return "login";
}

@PostMapping
public String login(@RequestParam String username, @RequestParam String password, Model model, HttpServletRequest request) {

    User user = null;
    try {
        user = this.authService.login(username, password);
        model.addAttribute("User", user);
        request.getSession().setAttribute("user",user);
        return "redirect:/home";
    } catch (InvalidArgumentsException exception) {

        model.addAttribute("hasError", true);
        model.addAttribute("error", exception.getMessage());
        return "login";
    }
}
}

As you see if the login is successful then I should be redirected to the home page but It doesn't happen I get redirected to the login page again and all that changes is the URL in it the parameters I've given are appended.如您所见,如果登录成功,那么我应该被重定向到主页,但它并没有发生,我再次被重定向到登录页面,所有更改都是其中的 URL,我给的参数被附加。 But when I use POSTMAN everything works just fine a POST request is sent and I get redirected to the /home page just like I've specified it in my Controller class. But I don't know why this wont happen when I use chrome.但是,当我使用 POSTMAN 时,一切正常,发送了一个 POST 请求,我被重定向到 /home 页面,就像我在 Controller class 中指定的那样。但我不知道为什么当我使用 chrome 时不会发生这种情况。 Having to use POSTMAN everytime I do a small change is really time-consuming.每次我做一个小改动都必须使用 POSTMAN 真的很耗时。 Also this is the HTML form这也是 HTML 表格

           <form id="form-id" th:method="POST" th:action="@{/login}">
            <div class="mb-3">
                <input  type="text" class="form-control" name="username" id="username"  
            aria-describedby="emailHelp"
                       placeholder="User Name">
            </div>
            <div class="mb-3">
                <input type="password" class="form-control" name="password" 
           id="password" placeholder="Password">
            </div>
            <!-- TODO use hasError set it in the model -->
            <div th:if="${hasError}">
            <span class="error text-danger" th:text="${error}"></span>
            </div>

            <div class="text-center"><button type="submit" class="btn btn-color px-5 mb- 
            5 w-100 btn btn-dark" >Login</button></div>
            </form>

I don't think there is something wrong with my code since everything works fine when I use POSTMAN but I really don't know why It wont work when I use my browser.我不认为我的代码有什么问题,因为当我使用 POSTMAN 时一切正常,但我真的不知道为什么当我使用我的浏览器时它不起作用。 Javascript is enabled in my browser I really don't know what seems to be the issue. Javascript 在我的浏览器中启用了我真的不知道是什么问题。 I also tried mapping the POST request to a different URL but still I get the same issue.我也尝试将 POST 请求映射到不同的 URL 但我仍然遇到同样的问题。 I really don't know what seems to be the issue我真的不知道是什么问题

As mentioned I can't send POST request using chrome but everything works fine with POSTMAN.如前所述,我无法使用 chrome 发送 POST 请求,但 POSTMAN 一切正常。

GET request instead of POST - As you can see a GET request is being instead where it Should be a POST request and here with Postman everything works fine I get redirected to the main page: Postman request I really don't know what to do as mentioned I cannot continue to work on the project because I need the user's session I also uploaded the project on gitlab you can find it here: https://gitlab.com/nisizenuni/betstar GET 请求而不是 POST - 如您所见,GET 请求代替了它应该是 POST 请求的地方,这里使用 Postman 一切正常我被重定向到主页: Postman 请求我真的不知道该怎么做提到我无法继续从事该项目,因为我需要用户的 session 我还在 gitlab 上上传了该项目,您可以在这里找到它: https://gitlab.com/nisizenuni/betstar

When using POST in spring forms, the values that you pass from the form are not passed as query parameters.在 spring forms 中使用 POST 时,您从表单传递的值不会作为查询参数传递。 This happens when you use GET method action in some form.当您以某种形式使用 GET 方法操作时会发生这种情况。

So this is the reason that postman works but your frontend does not work in combination with backend.所以这就是 postman 工作但你的前端不能与后端结合工作的原因。 In postman you send the request correctly as you manually define the request parameters and so the controller can serve the request correctly.在 postman 中,您在手动定义请求参数时正确发送了请求,因此 controller 可以正确地为请求提供服务。 Your frontend however does not send the values as query parameters and so your controller does not correctly serve the request.但是,您的前端不会将值作为查询参数发送,因此您的 controller 无法正确满足请求。

You can correct this by making the following changes.您可以通过进行以下更改来更正此问题。

First create a simple DTO object, where Spring could bind the passed values, which arrive in the body of the JSON request.首先创建一个简单的 DTO object,其中 Spring 可以绑定传递的值,这些值到达 JSON 请求的主体。

public class UserInfo {

  private String userame;
  private String password;

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

} 

Then you can define your controller as然后您可以将 controller 定义为

@PostMapping
public String login(@ModelAttribute UserInfo userInfo, Model model, HttpServletRequest request) {

String username = userInfo.getUsername();
String password = userInfo.getPassword();

.... same as the existing controller that you have

}

Also in Frontend you need to define your form as同样在前端,您需要将表单定义为

<form id="form-id" th:method="POST" th:action="@{/login}" th:object="${userInfo}">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有 csrf 标头的 Spring-Boot Post 请求 - Spring-Boot Post request with csrf header 通过Axios发送发布请求会在Spring-Boot后端生成一个空的RequestBody。 在Postman中工作,但不能通过Axios发布请求工作 - Sending a post request through Axios is generating an empty RequestBody in Spring-Boot backend. Works in Postman but not through Axios post request 将POST请求发送到服务器,服务器的响应为null,Angular-Spring-boot - Send POST request to server, Server's response is null, Angular - Spring-boot 无法从 postman 向我的 docker spring-boot 容器发送请求 - Can't send request from postman to my docker spring-boot container 为什么在尝试将PUT请求从angular5发送到spring-boot时得到未授权? - Why i'm getting Unauthorized when trying to send PUT request from angular5 to spring-boot? 使用 Java/Spring-boot 的 POST 请求端点 - POST request endpoint using Java/Spring-boot 在Spring Boot中使用自签名https无法验证Postman的POST / GET请求方法 - Can't authenticate a POST/GET request method from Postman, in Spring-boot with self-signed https 使用 Angular 2 和 Spring Boot 发送 post 请求 - Send post request with Angular 2 and Spring Boot 发送带有 JSON 数据 Spring 引导的 POST 请求 - Send POST request with JSON data Spring boot spring-boot 得到“400 bad request”错误,详细信息如下 - spring-boot get a “400 bad request” error, the detail is followed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM