我正在Http
请求Angular 4
使用它的内置HttpClient
类。一切正常,但是返回的错误变量有一个小问题。
当我post
使用错误的用户名/密码组合登录用户时,返回的错误对象包含一个error
键,该键的值应该是一个,Object
但我得到的是一个string
。请查看下面对象中 ** 之间的文本以了解问题所在:
{**error: "{"non_field_errors":["Unable to login with provided credentials."]}"**, headers: Object, status: 400, statusText: "Bad Request", url: "http://mymedicine.loc/api/auth/login/", ok: false, name: "HttpErrorResponse", message: "Http failure response for http://project.loc/api/auth/login/: 400 Bad Request"}
什么可能导致这种问题?我需要在那里有一个对象,这样我才能让用户知道身份验证过程中出了什么问题。我需要准确说明,当提供正确的凭据并且我已在发送请求CORS
的网络服务器上激活时,身份验证工作良好。HTTP
以下是提取的一小段代码,用于执行POST
http 请求:
interface ErrorMessage {
username: string[1];
password: string[1];
non_field_errors: string[1];
}
interface ErrorResponse {
error: ErrorMessage;
}
// get token from the server
this.http.post<TokenResponse>('http://myproject.loc/api/auth/login/', body).subscribe(
(res: TokenResponse) => {
// login with token
this.auth.login(res.auth_token);
// redirect
this.router.navigateByUrl('/url');
},
(err: ErrorResponse) => {
let error = err.error;
console.dir(err);
}
);