Ionic 2 Http Post - Response data


#1

I am trying to get response of the server (php) in Ionic 2.

In Ionic 1 we had:

          $http({
              method: "post", 
              link, 
              data
            })                
               .success(function(data) {
                    accessToken = data.access_token;
                })

Now I am trying for Ionic 2:

this.http.post(link, data)
   
        .subscribe(data =>  { this.data = data.access_token;
});

But this.data gets me no response…


#2

same for me :slight_smile:


#3

data.json()

text text text


#5

Problem Solved:

this.data = data._body;


#6
this.http.post(link, data)
   .map(res => res.json())
   .subscribe( (data) =>  { this.data = data.access_token; });

#7

Ya It’s perfectly, But how to set display object item on ionic tag , like i want to display userId on label Please check my below example:

Home.ts File

postRequest() {
 var headers = new Headers();
    headers.append("Accept", 'application/json');
    headers.append('Content-Type', 'application/json' );
    let options = new RequestOptions({ headers: headers });

    let postParams = {
      
        mode:'loginUser',
        userName:'anilpthummar',
        password:'123'
    }
    


 if (this.users) {
    // already loaded data
    return Promise.resolve(this.users);
  }
 
  return new Promise(resolve => {
    this.http.post("http://test12324/admin/webservice.php", postParams , options)
      .map(res => res.json())
      .subscribe(data => {
         this.users = data.user;
      
 console.log('this.users.userId-:::: -',this.users.userId); 

if(this.users == 1) {

        console.log('this.userId---',this.users); 
        
}else
{    
     console.log('DATA NOT FOUND ---'); 
}        
        
        resolve(this.users);
      });
  }); 

}

I want to set userId on label
Please help me.

Home.html File

<ion-list>

        <<button ion-button item-right (click)="postRequest()">Button</button>-
        <ion-item>


            <h1>{{ user.userId }}</h1>

            <!--<h1>{{user.userId}}</h1>-->
        </ion-item>

        <!--<h2>{{myUID.userId}}</h2>-->
        <!--<h2> {{user.userId}}</h2>-->

        <!--</ion-item>-->
    </ion-list>

Declaration error (this)