json响应angular2无法获得密钥

时间:2018-01-15 05:48:18

标签: json angular

我得到了以下数据作为回应,我需要达到错误键。任何人都可以指导我如何到达那里并以错误的形式在UI上显示它。

{"error":{"errors":[{"location":"body","param":"email","msg":"Email cannot be empty"},{"location":"body","param":"email","msg":"Email is not valid"}]}}

提前致谢

 if (loginModel.username && loginModel.password) {
      this.loginService.login(loginModel).subscribe((resp) => {
        this.loginService.authStorage.setAuthorizationHeader(resp['success']);
        if (resp['success']) {
          this.router.navigate(['dashboard']);
        }
        this.invalidUsernamePassword = true;
        console.log(JSON.stringify(resp['error']));
      }, (err) => {
        console.log(err);
      });

我使用了这个角度代码。

1 个答案:

答案 0 :(得分:0)

data = { "error": { "errors": [{ "location": "body", "param": "email", "msg": "Email cannot be empty" }, { "location": "body", "param": "email", "msg": "Email is not valid" }] } };

如果回复位于data属性中,则可以通过

进行访问
this.data.error.errors.forEach(error => {
  console.log(error.msg);
});