Angular5 HTTPclient遇到嵌套json

时间:2018-03-14 18:25:53

标签: json httpclient angular5

如果数据是嵌套的json格式,则Angular 5中的HttpClient.get不返回数据。下面的代码适用于非嵌套的json数据。 如果我遗漏任何内容或获取数据的方法,请您告诉我吗?

代码:

import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';

export class ReportService {...
  searchByUser(userUrl: string): Observable<any> {
    console.log('userUrl', userUrl);

    return this.httpClient
                .get(userUrl, { responseType: 'json'  })                                
                .catch((error: HttpErrorResponse) => {
                  return Observable.throw(error.status)
                });
  }


}
import { ReportService } from '../report.service';

this.reportService.searchByUser(userUrl).subscribe(data => {
      console.log("Data :", data);
    });

userUrl返回的嵌套JSON示例数据:

[
{
"userId": "JX",
"location": "CHANTILLY, XX",
"fullName": "SMITH, JX L",
"userType": "P",
"userStatusDesc": "Active",
"occupationInfo": {
"occupationTitle": "HR GENERALIST HQ"
},
"miscInfo": {
"rankingPoints": 15,
"searchTermCount": 1
}
}
]     

1 个答案:

答案 0 :(得分:0)

看起来问题不在于HttpClient或嵌套的JSON。我能够使用本地生成的相同JSON。当我部署代码时,它使用在同一代理下生成的JSON。