我試圖連接我的反應本地登錄與後端。我用愛可信^ 0.16.2調用API,但它總是返回我「錯誤:請求與狀態代碼的失敗400」 我有什麼:陣營本地愛可信始終返回「請求失敗,狀態碼400」
export const loginUser = ({ email, password }) => {
return (dispatch) => {
axios.post('http://localhost:8080/api/users/login',
{
email,
password
},
{
Accept: 'application/json',
'Content-Type': 'application/json',
}
)
.then(response => {
console.log(response);
dispatch({
type: LOGIN_USER,
payload: response.data.status
});
Actions.home();
})
.catch(error => {
console.log(error);
dispatch({
type: LOGIN_USER,
payload: error
});
}
);
};
};
編輯: 很肯定請求體是正確的,因爲該數據庫具有所有正確的信息。
我重視我這裏有API,我使用的Java春季啓動:
@Controller
@RequestMapping(value = "api/users", produces = {MediaType.APPLICATION_JSON_VALUE})
public class UserController {
/**
* Log a user in
* @param request
* @return
* @throws DuplicatedUserException
* @throws InvalidRequestException
*/
public static class UserLoginRequest implements ValiatedRequest {
private String username;
private Integer gmtShift;
private String email;
private String password;
@Override
public void validate() throws InvalidRequestException {
if (email == null || email.isEmpty()) {
throw new InvalidRequestException("email is empty.");
}
if (gmtShift == null || gmtShift < -12 || gmtShift > +12) {
throw new InvalidRequestException("gmtShift is empty or invalid.");
}
if (username == null || username.isEmpty()) {
throw new InvalidRequestException("user name is empty.");
}
if (password == null || password.isEmpty()) {
throw new InvalidRequestException("password is empty.");
}
if (!RegexUtil.EMAIL.matcher(email).find()) {
throw new InvalidRequestException("email is invalid.");
}
if (password.length() > 32) {
throw new InvalidRequestException("password cannot be longer than 32 characters.");
}
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getGmtShift() {
return gmtShift;
}
public void setGmtShift(Integer gmtShift) {
this.gmtShift = gmtShift;
}
}
@RequestMapping(value = "login", consumes = {MediaType.APPLICATION_JSON_VALUE}, method = RequestMethod.POST)
@ResponseBody
public Response loginUser(
@RequestBody UserLoginRequest request) throws AuthenticationException,
InvalidRequestException {
request.validate();
userService.authenticate(RequestContextHolder.currentRequestAttributes().getSessionId(),
request.getGmtShift(),
request.getUsername(),
request.getEmail(),
request.getPassword());
return new Response(Status.Success);
}
}
}
可能與你的api有關。請添加您的API的相關代碼。 – bennygenel
您確定信息正確地發送到API?發佈完整的請求信息後,您可以通過使用React Native Debugger或Chrome Inspector檢查JS,然後在Network選項卡中單擊所需的請求來獲取它。 –
嘗試使用郵差來檢查是否如預期般的API的返回結果,再加入一些console.logs調試無論是API或你愛可信。 https://www.getpostman.com/ – soutot