Method ‘GET’ is not supported.

huangapple go评论123阅读模式
英文:

Method 'GET' is not supported

问题

当我发送HTTP POST请求时,屏幕上出现了这个消息。我该如何解决这个问题?我没有使用任何GET注解或请求。

HTPP 请求:http://localhost:9000/user/register?username=a&password=1

控制台错误

> 无法加载资源:服务器响应状态为405(方法不允许)

JavaScript

  1. <script>
  2. document.getElementById("signupbtn").addEventListener("click",function(){
  3. const username = document.getElementById("username").value;
  4. const password = document.getElementById("password").value;
  5. const xhr = new XMLHttpRequest();
  6. xhr.open("POST","http://localhost:9000/user/register?username="+username+"&password="+password);
  7. xhr.send();
  8. })
  9. </script>

控制器

  1. @PostMapping("/register")
  2. ResponseEntity<String> addUser(@RequestParam("username") String username,@RequestParam("password") String password) {
  3. userService.addUser(username, password);
  4. return ResponseEntity.ok("用户已成功添加");
  5. }

服务

  1. public void addUser(String username,String password){
  2. userRepository.save(new User(username,password));
  3. }
英文:

When I make HTTP POST request, I got this message on my screen. How can I solve this problem? I don't use any GET annotation or request.

HTPP REQUEST : http://localhost:9000/user/register?username=a&password=1

Console Error

> Failed to load resource: the server responded with a status of 405 (Method Not Allowed)

JavaScript

  1. <script>
  2. document.getElementById("signupbtn").addEventListener("click",function(){
  3. const username = document.getElementById("username").value;
  4. const password = document.getElementById("password").value;
  5. const xhr = new XMLHttpRequest();
  6. xhr.open("POST","http://localhost:9000/user/register?username="+username+"&password="+password);
  7. xhr.send();
  8. })
  9. </script>

Controller

  1. @PostMapping("/register")
  2. ResponseEntity<String> addUser(@RequestParam("username") String username,@RequestParam("password") String password) {
  3. userService.addUser(username, password);
  4. return ResponseEntity.ok("User added succesfully");
  5. }

Service

  1. public void addUser(String username,String password){
  2. userRepository.save(new User(username,password));
  3. }

答案1

得分: 1

问题出在当我发送请求时。这是一个GET请求而不是POST。我将PostMapping更改为GetMapping问题得到解决。我认为关于POST和GET映射存在误解。

英文:

The problem is when i send to request. This is a GET request not POST . I changed the PostMapping To GetMapping problem is solved. I think there is missunderstanding about POST and GET mapping

答案2

得分: 0

这是来自REQBIN的解决方案。

请参考链接并执行以下代码并应用到您的项目中:

  1. let xhr = new XMLHttpRequest();
  2. xhr.open("POST", "https://reqbin.com/echo/post/json");
  3. let data = {
  4. "Id": 78912,
  5. "Customer": "Jason Sweet",
  6. };
  7. xhr.onload = () => console.log(xhr.status);
  8. xhr.send(data);
英文:

Here is solution from the REQBIN

Please refer the link and execute the code and apply in your project

  1. let xhr = new XMLHttpRequest();
  2. xhr.open("POST", "https://reqbin.com/echo/post/json");
  3. let data = {
  4. "Id": 78912,
  5. "Customer": "Jason Sweet",
  6. };
  7. xhr.onload = () => console.log(xhr.status);
  8. xhr.send(data);

答案3

得分: 0

你的代码没有问题,除非你的应用安全配置中有限制,比如(allowedMethods)。

英文:

there is nothing wrong with your code , it should work unless you do have restriction in your app security config like (allowedMethods )

huangapple
  • 本文由 发表于 2023年7月31日 20:38:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定