我正在嘗試將資料從 Vue 發送到 Spring
我一直在到處尋找,一切看起來都很完美,但仍然不起作用
起初我在想可能是因為我后面的班級比我從前面發送的班級有更多的欄位,但即使我創建了另一個具有相同欄位的班級,它也不起作用
這是我的發送請求:
sendRequest() {
console.log(this.user);
(this.loading = true),
axios.get("http://localhost:8081/user/log-in", this.user)
.then((response) => {
console.log(response);
})
.catch((error) => {
(this.isShow = true),
(this.isHide = false),
(this.showAlert = true),
(this.message = error.response.data);
(this.showMessage = true),
setTimeout(
function () {
this.showMessage = false;
(this.isShow = false), (this.isHide = true);
}.bind(this),
3000
);
})
.finally(() => {
this.loading = false;
});
這是我從前向后發送的資料:
user: {
userName: "",
password: ""
},
這是我獲取資料的后端類:
public class UserSignUpDTO {
private String firstName;
private String userName;
private String email;
private String password;
private String birthDay;
}
這是獲取資料的方法:
@GetMapping("/log-in")
public UserDTO logInUser(@RequestBody UserSignUpDTO user) throws Exception {
return userService.logInUser(user);
}
請求映射:
@RestController
@RequestMapping("/user")
public class UserController {
uj5u.com熱心網友回復:
不建議將 GET 與請求正文一起使用。雖然HTTP/1.1似乎沒有明確禁止這一點,但它應該沒有意義。
正如這里所暗示的,
如果您通過在服務器上決議它并根據其內容更改回應來賦予它含義,那么您將忽略 HTTP/1.1 規范第 4.3 節中的此建議。
在這種情況下,我建議您使用 POST 請求。
改變這個
axios.get("http://localhost:8081/user/log-in", this.user)
對此
axios.post("http://localhost:8081/user/log-in", this.user)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/404706.html
標籤:
