java 不支持方法“GET”

lsmepo6l  于 10个月前  发布在  Java
关注(0)|答案(3)|浏览(148)

当我发出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));
       
}

9w11ddsr

9w11ddsr1#

以下是REQBIN的解决方案
请参考链接,并执行代码,并在您的项目中应用

let xhr = new XMLHttpRequest();

xhr.open("POST", "https://reqbin.com/echo/post/json");

let data = {
  "Id": 78912,
  "Customer": "Jason Sweet",
};

xhr.onload = () => console.log(xhr.status);

xhr.send(data);

字符串

2nc8po8w

2nc8po8w2#

问题是当我发送请求时。这是一个GET请求,而不是POST。我把PostMapping改成GetMapping问题就解决了。我认为对POST和GETMap存在误解

carvr3hs

carvr3hs3#

你的代码没有任何问题,它应该工作,除非你在你的应用程序安全配置中有限制,如(allowedMethods)

相关问题