Response of Http Api call promise is undefined - Angular 2

喜夏-厌秋 提交于 2019-12-02 07:19:16
k11k2

Put the console.log inside the data function.

Could you try like this.

export class AppComponent implements OnInit  { 

  data2: String;
  constructor(private s: DemoService){} 

  ngOnInit(){

    this.s.GetHttpData().subscribe(data=>{
        this.data2=data;
        console.log("Http call  completed: "+this.data2)
    });

}

Try to work with a simple promise here.

In Service.ts (DemoService)

 GetHttpData() {

        return new Promise(resolve => {

            this.http.get('http://localhost:54037/api/home')
                .map(res => res.json())
                .subscribe(data => {
            resolve(data);
        });
    }

And in Component:

this.s.GetHttpData()
        .then(data => { 
             console.log("Http call  completed: "+data);
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!