我从http发布请求中得到了以下响应。我很难提取"消息"从身体

时间:2017-07-19 19:49:54

标签: java angular rest http http-post

消息如下所示: 具体如下:" {"时间戳":1500492972729,"状态":500,"错误":"内部服务器错误",& #34; exception":" java.lang.RuntimeException"," message":"用户名已经存在","路径": " /注册"}"

this.userService.create(this.model)
        .map((response: Response) =>{
          const data = response.json();
          const extractedChars = data.results;
          const chars = extractedChars.map((char) =>{
            return { message: char.message}
          });
          return chars;
        })
        .subscribe(

            (data) => {
              console.log(data);
                this.alertService.success('Registration successful', true);
                this.router.navigate(['/login']);
            },
            (error) => {
                this.alertService.error(error);
                this.loading = false;
            }
            );

Image of response error

1 个答案:

答案 0 :(得分:0)

我通过使用json()然后使用.message来引用消息来解决这个问题。 json()将它转换为对象。

(error) => {
              const results = error.json();
              this.alertService.error(results.message);
              this.loading = false;
             }