I'm trying to return information from my api but I don't understand how to correctly use subscribe. With an array I return an empty array from my service and push values into it as I get them.
How would I correctly return just a single value variable in app-ponent ts. Right now If I do a alert(JSON.stringify(authenticated))
it just gives me {"_isUnsubscribed":false,"_subscriptions":[{"isUnsubscribed":false}]}
app-ponent ts
checkAuthentication(){
var authenticated = this._authService.getAuthenication();
}
authentication Service
getAuthenication() {
return this._http.get('auth.php').subscribe(res => {
if (res.json().authenticated === true){
return true;
}
return false;
});
}
auth.php
<?
session_start();
$authenticated = ( isset($_SESSION["authenticated"]) ? true : false );
$post_data = json_encode(array(authenticated => $authenticated));
echo $post_data;
?>
I'm trying to return information from my api but I don't understand how to correctly use subscribe. With an array I return an empty array from my service and push values into it as I get them.
How would I correctly return just a single value variable in app-ponent ts. Right now If I do a alert(JSON.stringify(authenticated))
it just gives me {"_isUnsubscribed":false,"_subscriptions":[{"isUnsubscribed":false}]}
app-ponent ts
checkAuthentication(){
var authenticated = this._authService.getAuthenication();
}
authentication Service
getAuthenication() {
return this._http.get('auth.php').subscribe(res => {
if (res.json().authenticated === true){
return true;
}
return false;
});
}
auth.php
<?
session_start();
$authenticated = ( isset($_SESSION["authenticated"]) ? true : false );
$post_data = json_encode(array(authenticated => $authenticated));
echo $post_data;
?>
Share
Improve this question
asked Feb 20, 2016 at 14:20
ClickThisNickClickThisNick
5,2509 gold badges47 silver badges70 bronze badges
1 Answer
Reset to default 7change
return this._http.get('auth.php').subscribe(res => {
to
return this._http.get('auth.php').map(res => {
and call it like
this._authService.getAuthenication()
.subscribe(value => this.authenticated = value);