最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeScript Angular 2 Responses Subscribing - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 7

change

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);
发布评论

评论列表(0)

  1. 暂无评论