我正在尝试从api获取JSON数据并使用angular 2显示其内容。 会话组件:
export class ConversationComponent implements OnInit {
public conversation: any;
constructor(private conversationsService: ConversationsService,
private routeParams: RouteParams) { }
ngOnInit() {
let id = +this.routeParams.get('id');
this.conversationsService.getConversation(id)
.subscribe(
con => this.conversation = con,
error => console.log(this.conversation)
)
}
}
ComversationsService:
@Injectable()
export class ConversationsService {
private url = 'https://dev4.aldensys.com/PrometheusWebAPI/api/Conversations/';
private token = this.storageService.getAuthorizationToken();
constructor(private http: Http, private storageService: StorageService) { }
getConversation(id: number) {
var headers = new Headers();
headers.append('Authorization', 'Bearer ' + this.token);
var options = new RequestOptions({ headers: headers });
return this.http.get(this.url + id, options)
.map(res => res.json())
.catch(this.handleError);
}
private handleError(error: any) {
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg); // log to console instead
return Observable.throw(errMsg);
}
}
出于某种原因,这种对话是未定义的。在我将其映射到服务文件上的json之后,我试图立即发出响应警告:它显示了' object:Object'
非常感谢任何帮助。
答案 0 :(得分:2)