【发布时间】:2018-01-21 00:13:47
【问题描述】:
我有一个登录端点,我使用 Angular 的 Http.get 向该端点发送请求。当我发送不正确的登录凭据时,我从服务器收到带有错误代码 401 的 JSON 响应。我似乎无法在错误回调的错误中找到 JSON 响应。这是一个使用 Angular 4 的 ionic 3 应用程序。如何从 Angular Http 给我的错误中获取我在 PostMan 中看到的响应正文?
网址
http://localhost:8080/v1.0/login?rolename=Customer&username=customer00&password=wrongpass
邮递员响应正文
{
"status": "login failed - bad credentials"
}
console.log("登录出错:" + JSON.stringify(error, undefined, 2));
Error in login: {
"_body": {
"isTrusted": true
},
"status": 0,
"ok": false,
"statusText": "",
"headers": {},
"type": 3,
"url": null
}
【问题讨论】:
-
您是否尝试过拨打
error.json()而不是JSON.stringify(error, undefined, 2)? -
AFAIK,
$http用于 angularjs 和 ionic1,来自 ionic2+,您应该使用来自HttpModule的Http的@angular/http。 -
@Pengyy 我正在使用@angular/http。这是我的问题中的一个错字,对此感到抱歉。我会更新的。
-
@KimKern error.json() 给了我这个输出
Error in login: { "isTrusted": true } -
我遇到了完全相同的问题,最新的 ionic (angular 4.4.3)
标签: angular ionic3 angular-http