英文:
Method 'GET' is not supported
问题
当我发送HTTP POST
请求时,屏幕上出现了这个消息。我该如何解决这个问题?我没有使用任何GET
注解或请求。
HTPP 请求:http://localhost:9000/user/register?username=a&password=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("用户已成功添加");
}
服务
public void addUser(String username,String password){
userRepository.save(new User(username,password));
}
英文:
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
<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>
Controller
@PostMapping("/register")
ResponseEntity<String> addUser(@RequestParam("username") String username,@RequestParam("password") String password) {
userService.addUser(username, password);
return ResponseEntity.ok("User added succesfully");
}
Service
public void addUser(String username,String password){
userRepository.save(new User(username,password));
}
答案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的解决方案。
请参考链接并执行以下代码并应用到您的项目中:
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);
英文:
Here is solution from the REQBIN
Please refer the link and execute the code and apply in your project
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);
答案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 )
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论