当我发出HTTP POST
请求时,屏幕上出现了此消息。我该如何解决这个问题?我不使用任何GET
注解或请求。
HTPP请求:http://localhost:9000/user/register?用户名=a&密码=1
控制台错误
无法加载资源:服务器的响应状态为405(不允许方法)
JavaScript
<script>
document.getElementById("signupbtn").addEventListener("click",function(){
const username = document.getElementById("username").value;
const password = document.getElementById("password").value;
const xhr = new XMLHttpRequest();
xhr.open("POST","http://localhost:9000/user/register?username="+username+"&password="+password);
xhr.send();
})
</script>
字符串
控制器
@PostMapping("/register")
ResponseEntity<String> addUser(@RequestParam("username") String username,@RequestParam("password") String password) {
userService.addUser(username, password);
return ResponseEntity.ok("User added succesfully");
}
型
维修保养
public void addUser(String username,String password){
userRepository.save(new User(username,password));
}
型
3条答案
按热度按时间9w11ddsr1#
以下是REQBIN的解决方案
请参考链接,并执行代码,并在您的项目中应用
字符串
2nc8po8w2#
问题是当我发送请求时。这是一个GET请求,而不是POST。我把PostMapping改成GetMapping问题就解决了。我认为对POST和GETMap存在误解
carvr3hs3#
你的代码没有任何问题,它应该工作,除非你在你的应用程序安全配置中有限制,如(allowedMethods)