# Problem whit $http.get and json

**URL:** <https://forum.ionicframework.com/t/problem-whit-http-get-and-json/43432>\
**Category:** ionic-v1\
**Created:** [February 11, 2016, 11:28am UTC](https://forum.ionicframework.com/t/problem-whit-http-get-and-json/43432 "2016-02-11T11:28:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Riidio](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/riidio/32/110711_2.png) [@Riidio](https://forum.ionicframework.com/u/Riidio)\
**Post date:** [February 11, 2016, 11:28am UTC](https://forum.ionicframework.com/t/problem-whit-http-get-and-json/43432/1 "2016-02-11T11:28:14Z")

</div>

Hi, i have this code:

-SERVICES.JS

```
serv.factory('loadData', function($http) {

  var test = [];
  var uri = 'http://jsonplaceholder.typicode.com/posts';

  $http.get(uri).then(function(response) {
      test = response.data;
    });

	return {
		all : function() {
        return test;
    }
	};

});

```

-CONTROLLER.JS

```
ctrl.controller('HomeCtrl', function($scope, $ionicPopup, loadData) {

 $scope.prova = loadData.all();

});

```

-HOME.HTML

```
<ion-view view-title="Home">
  <ion-content>
    <h1>Home</h1>
{{prova}}
  </ion-content>
</ion-view>

```

In html code, my variable prova is null, i see in html only this []

where i wrong?

ps: in service if i add value a test, this work.  
i think problem is $http.get because not add value json a this variable

---

<div class="post-metadata">

**Author:** ![hemant\_Chaudhary](https://avatars.discourse-cdn.com/v4/letter/h/71c47a/32.png) [@hemant\_Chaudhary](https://forum.ionicframework.com/u/hemant_Chaudhary)\
**Post date:** [February 11, 2016, 12:05pm UTC](https://forum.ionicframework.com/t/problem-whit-http-get-and-json/43432/2 "2016-02-11T12:05:47Z")

</div>

try loadData.test instead of loadData.all();

---

<div class="post-metadata">

**Author:** ![Riidio](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/riidio/32/110711_2.png) [@Riidio](https://forum.ionicframework.com/u/Riidio)\
**Post date:** [February 11, 2016, 12:21pm UTC](https://forum.ionicframework.com/t/problem-whit-http-get-and-json/43432/3 "2016-02-11T12:21:40Z")

</div>

no work, problem is in $http.get

---

<div class="post-metadata">

**Author:** ![Riidio](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/riidio/32/110711_2.png) [@Riidio](https://forum.ionicframework.com/u/Riidio)\
**Post date:** [February 12, 2016, 1:13pm UTC](https://forum.ionicframework.com/t/problem-whit-http-get-and-json/43432/4 "2016-02-12T13:13:12Z")

</div>

upp upp…!  
nothing…?

---

<div class="post-metadata">

**Author:** ![coreelements](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@coreelements](https://forum.ionicframework.com/u/coreelements)\
**Post date:** [February 12, 2016, 1:23pm UTC](https://forum.ionicframework.com/t/problem-whit-http-get-and-json/43432/5 "2016-02-12T13:23:00Z")

</div>

The problem is …

`loadData.all();`

is just a function wich retrieves the contents of

`var test`

But you have to think async … your test var is still empty because $http is probably not ready yet executing.

The solution is to make a promise function in the factory. In the function execute the $http and then return the reject/resolve object back to the controller. If the controller recieves a resolve, fill the scope var with the result of the resolve object.

---

<div class="post-metadata">

**Author:** ![davguij](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/davguij/32/72807_2.png) [@davguij](https://forum.ionicframework.com/u/davguij)\
**Post date:** [February 12, 2016, 4:09pm UTC](https://forum.ionicframework.com/t/problem-whit-http-get-and-json/43432/6 "2016-02-12T16:09:07Z")

</div>

You need to call the $http method from within the factory returning function, like this:

```
serv.factory('loadData', function($http) {

  var uri = 'http://jsonplaceholder.typicode.com/posts';

	return {
		all : function() {
                   return $http.get(uri).then(function(response) {
                           return response.data;
                   });
            }
	};

});
```

---

<div class="post-metadata">

**Author:** ![Riidio](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/riidio/32/110711_2.png) [@Riidio](https://forum.ionicframework.com/u/Riidio)\
**Post date:** [February 12, 2016, 10:13pm UTC](https://forum.ionicframework.com/t/problem-whit-http-get-and-json/43432/7 "2016-02-12T22:13:15Z")

</div>

yes, this work.  
but if i want use variable?

---

<div class="post-metadata">

**Author:** ![coreelements](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@coreelements](https://forum.ionicframework.com/u/coreelements)\
**Post date:** [February 13, 2016, 1:52pm UTC](https://forum.ionicframework.com/t/problem-whit-http-get-and-json/43432/8 "2016-02-13T13:52:56Z")

</div>

what would the variable do?
