i can’t access to a key of a json response from a restful web service.
{"_body":"{\"values\": {\"user_id\":\"1\",\"name\":\"fred test\",\"email\":\"fred@test.test\",\"username\":\"fredtest\",\"token\":\"d5f66a06ec809d70d0c52842df8dc0011d7d1ad0f2d56f50d3123da17a2489fe\"}}","status":200,"ok":true,"statusText":"OK","headers":{"pragma":["no-cache"],"content-type":["text/html;charset=UTF-8"],"cache-control":["no-store"," no-cache"," must-revalidate"],"expires":["Thu"," 19 Nov 1981 08:52:00 GMT"]},"type":2,"url":"http://localhost/PHP-Slim-Restful/api/login"}
I would like to acces to ‘values’ in this function: (this.responseData.values)
login(){
console.log('login'+ this.userData);
// Your app login API web service call triggers
this.authService.postData(this.userData,'login').then((result) => {
this.responseData = result;
console.log('userdata : '+ temp);
if(this.responseData.values){
console.log('response: ' + this.responseData);
localStorage.setItem('userData', JSON.stringify(this.responseData));
this.navCtrl.push(TabsPage);
}
else{
this.showToastWithCloseButton()
}
}, (err) => {
console.log('erreur : '+err);
});
}
I have an error undifined!
Can you help me?
try
this.responseData = result.data;
thx but i have an error data is not defined …
can you provide me the function code ?
postData
import {Injectable} from '@angular/core';
import {Http, Headers} from '@angular/http';
import 'rxjs/add/operator/map';
let apiUrl = 'http://localhost/PHP-Slim-Restful/api/';
// let apiUrl = 'https://ternatsterker.dev:8890/rest_springerapp/user/';
@Injectable()
export class AuthService {
constructor(public http: Http) {
console.log('AuthService Provider construct');
}
postData(credentials, type) {
console.log('postData: '+ type + ' : ' + JSON.stringify(credentials));
return new Promise((resolve, reject) => {
let headers = new Headers();
console.log(headers);
this.http.post(apiUrl + type,JSON.stringify(credentials), {headers: headers})
.subscribe(res => {
console.log('response :'+ res);
resolve(res);
}, (err) => {
reject(err);
});
});
}
}
try this
by adding map function
this.http.post(apiUrl + type,JSON.stringify(credentials), {headers: headers})
.map((res1: Response) => res1.json())
.subscribe(res2 => {
console.log('response :'+ res2);
resolve(res2.data);
}, (err) => {
reject(err);
});
don’t forgot include Response in head of file
import {Http, Headers,Response} from '@angular/http'
thx ahmed, it work exactly the same way but i still can’t acces to this.responseData.values.
maybe the “_body” caused problem???
@sammycolson What is logged when you call console.log('response :', res);?
Also try to log the following: console.log('response json:', res.json()); and post here the result.
Tip: Use commas (,) instead of + in the log so that objects can be seen in a better way in the browser console.
I am having a problem with this script (PHP-Restful-api) i used to connect just fine with the database but now i am getting -data is null- i haven’t made any changes.
authservice
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
let apiUrl = "http://localhost/PHP-Slim-Restful1/api/";
@Injectable()
export class AuthService {
constructor(public http: Http) {
console.log('Hello Provider');
}
postData(credentials, type) {
return new Promise((resolve, reject) => {
let headers = new Headers();
this.http.post(apiUrl+type, JSON.stringify(credentials), {headers: headers}).
subscribe(res => {
console.log(res);
resolve(res.json());
}, (err) => {
reject(err);
});
});
}
}
any help? do you solved this sammy?