Response of Http Api call promise is undefined - Angular 2

后端 未结 2 383
滥情空心
滥情空心 2021-01-27 04:15

I have created an api in WebAPI as below.

public HttpResponseMessage Get() {

            var response = Request.CreateResponse(HttpStatusCode.OK);
            r         


        
2条回答
  •  遇见更好的自我
    2021-01-27 04:34

    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);
    });
    

提交回复
热议问题