我正在使用 Spring Boot MVC 開發一個應用程式,并且我有一個登錄頁面,每當我使用 chrome 在表單上輸入資料時,我的瀏覽器不會將我重定向到我在 Controler 類中指定的頁面,而是發送一個 GET請求它應該發送 POST 請求的位置。這是我的控制器類
@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) {
User user = null;
try {
user = this.authService.login(username, password);
model.addAttribute("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. 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. Having to use POSTMAN everytime I do a small change is really time-consuming. Also this is the HTML form
<form id="form-id" th:action="@{/login}" th:method="post">
<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>
我認為我的代碼沒有問題,因為當我使用 POSTMAN 時一切正常,但我真的不知道為什么當我使用瀏覽器時它不會作業。我的瀏覽器中啟用了 Javascript 我真的不知道問題出在哪里。我還嘗試將 POST 請求映射到不同的 URL,但仍然遇到同樣的問題。
uj5u.com熱心網友回復:
我也相信您的代碼示例沒有問題,因為如果有問題,它不應該從 POSTMAN 正確回應,除非您配置了應用程式服務器并限制了來自瀏覽器的請求。我可以給你的建議是在使用 Fiddler(Fiddler 是一個強大的 Web 除錯代理)等工具發送請求時監控請求,看看兩個請求是否向服務器發送相同的引數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/444875.html
上一篇:如何從資產中的config.json檔案中獲取影像url并以角度在index.html檔案中顯示它
下一篇:瀏覽器快取如何確定請求是否相同?
