从HTTP catch返回解析错误 - Angular 2

Syd*_*ria 7 angular2-services angular

我已经实现了通过我的HTTP请求中的catch获取API错误返回的值.我现在的问题是,当我调用服务时,如何获取组件中HTTP catch的返回值.

这是我的HTTP服务中的代码:

login(username,password){
        let headers = new Headers();
        headers.append('Content-Type','application/json');

        return this.http.post(this.configEnvironment.url() + "oauth/access_token",
            JSON.stringify(
                {
                    username: username,
                    password: password,
                    grant_type: "password",
                    client_id: "xxxx",
                    client_secret: "xxxxx"
                }
            ),
            { headers }
        )
        .map(res => res.json())
        .catch((err:Response) => {
            let details = err.json();
            return Observable.throw(new Error(details));
         });

     }
Run Code Online (Sandbox Code Playgroud)

这是我的login.component中的代码:

this.loginService.login(this.model.username,this.model.password)
            .subscribe(
                data => console.log("data"),
                error => {
                   console.log(error);
                },
                ()  =>  console.log("Finished")
            );
Run Code Online (Sandbox Code Playgroud)

在chrome开发人员工具中,这是console.log的返回:

Error: [object Object](…)
Run Code Online (Sandbox Code Playgroud)

但是http服务catch的实际返回是:{"error":"invalid_credentials","error_description":"invalid credentials"}这是我想进入login.component

Bee*_*ice 8

在你的.catch(),改变:

return Observable.throw(new Error(details));
Run Code Online (Sandbox Code Playgroud)

至:

return Observable.throw(details);
Run Code Online (Sandbox Code Playgroud)