# Getting data from http for every page

**URL:** <https://forum.ionicframework.com/t/getting-data-from-http-for-every-page/104866>\
**Category:** ionic-v3\
**Created:** [September 10, 2017, 6:44pm UTC](https://forum.ionicframework.com/t/getting-data-from-http-for-every-page/104866 "2017-09-10T18:44:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zabka229](https://avatars.discourse-cdn.com/v4/letter/z/87869e/32.png) [@zabka229](https://forum.ionicframework.com/u/zabka229)\
**Post date:** [September 10, 2017, 6:44pm UTC](https://forum.ionicframework.com/t/getting-data-from-http-for-every-page/104866/1 "2017-09-10T18:44:49Z")

</div>

Hello I want to download the json data from server and then assign the data to the globally available variable.  
I did something like this:

```auto
import {Injectable} from '@angular/core';  
import { Http, Headers, RequestOptions } from '@angular/http';

@Injectable()
export class GlobalService {  

    public loginState:boolean = false;
    public player: any = [];

    constructor(public http: Http) {

      this.http.get('http://localhost/ionic/retrieve-data.php')
      .map(res => res.json())
      .subscribe(data =>
      {
        this.player = data;
        console.log(this.player.user);
      });
    }

}

```

and in other pages i inject it in constructor, but all i can see is empty variable player, loginstate is working.  
How can I get the data ti the variable, so I can see this in other pages?

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [September 10, 2017, 11:54pm UTC](https://forum.ionicframework.com/t/getting-data-from-http-for-every-page/104866/2 "2017-09-10T23:54:33Z")

</div>

My rule is “only subscribe in pages”, so I would change your service to expose a `getPlayer()` method that returns an `Observable<Player>` and then subscribe to that in each interested page. Organizing things this way designs away race conditions such as you have here.

---

<div class="post-metadata">

**Author:** ![zabka229](https://avatars.discourse-cdn.com/v4/letter/z/87869e/32.png) [@zabka229](https://forum.ionicframework.com/u/zabka229)\
**Post date:** [September 11, 2017, 7:38pm UTC](https://forum.ionicframework.com/t/getting-data-from-http-for-every-page/104866/3 "2017-09-11T19:38:39Z")

</div>

Can you explain me more how it should look?

I made it in app.component and then saved it to global.service variable and it works, but I think there is other better solutions.

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [September 11, 2017, 7:59pm UTC](https://forum.ionicframework.com/t/getting-data-from-http-for-every-page/104866/4 "2017-09-11T19:59:22Z")

</div>

I don’t think I can say much more than I already have at this point. If you would like to post more of what you have, then perhaps I could.
