$http.get not executing functions on success


#1

Hi all,

I am doing my first ionic project where i have to get some data from database and on success I havet to (1)store it in a variable and (2)navigate to a page where this variable is used.

The page is navigating, but data are not sent.

Here is my search.html code ;

<body ng-app="starter" ng-controller="ListCtrl">
<ion-pane>
    <ion-header-bar class="bar-stable">
        <h1 class="title">Real Estate</h1>
    </ion-header-bar>
    <ion-content>
        <h4 align="center"> Enter Place or Pincode </h4>
        <div class="list list-inset" >
            <label class="item item-input">
                <input type="text" name="place" ng-model="data.place" value="" placeholder="Place"  ng-focus="$scope.placefocus=true;data.pincode=null" ng-blur="$scope.placefocus=false" ng-disabled = "$scope.pincodefocus">
           </label>
           <label class="item item-input">
               <input type="number" name="pincode" ng-model="data.pincode" value="" placeholder="Pin Code" ng-focus="$scope.pincodefocus=true;data.place=null" ng-blur="$scope.pincodefocus=false" ng-disabled="$scope.placefocus">
           </label>
       </div>
       <button class="button button-block button-positive"  ng-click="getdata()">
           Search
       </button>
   </ion-content>
</ion-pane>
</body>

And here is my controller code :

app.controller('ListCtrl',function($scope,$http,$location,$window){
$scope.data = {};
$scope.getdata = function(){
    //alert($scope.data.place);
    $http.get("http://localhost/angular/data.php")  
                                                      //,{'place':$scope.data.place,'pincode':$scope.data.pincode})
        .success(function(response,status,headers,config){
            alert(response);
            $scope.datas=response;
            $scope.navig('/show.html');
        })
}
$scope.navig = function(url){
    $window.location.href=url;
};
});

and here is my show.html page :

<ion-content>
<div class="list card" ng-repeat="site in datas">
<div class="item item-body">
    <a href="#" class="subdued"><img class="full-image" src="img/{{site.image}}" height="150px">
        <p>
            RS.{{site.price}} &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp <span> {{site.square_feet}} sq.ft &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</span><span>{{site.bed}} BHK</span>
        </p>
    </a>
</div>
</div>
</ion-content>

alert(response) in the controller code alerts [object Object],[object Object],[object Object]
But the output page (show.html) is blank

If i call navig("/show.html") directly from the button click (in search.html) instead of getdata() and change the contrloller code to the below one, I am getting the result:( But I cannot execute this way because I have to get data from database for particular place and pincode entered)

app.controller('ListCtrl',function($scope,$http,$location,$window){
    $scope.data = {};
    $http.get("http://localhost/angular/data.php")
                                                  //,{'place':$scope.data.place,'pincode':$scope.data.pincode})
        .success(function(response,status,headers,config){
            alert(response);
            $scope.datas=response;
        })
    $scope.navig = function(url){
        $window.location.href=url;
    };
});

Please help


#2

if you see [Object object], try to do a

console.log(response), 

or a

alert(JSON.stringify(response))

This way, you will be able to see your results I think


#3

Hi JC_cap,
thanks for the reply,

when I tried console.log(response), it shows [Object,Object,Object].

And when I added alert(JSON.stringify(response)), it alerted the data required in a json format.So how can I use this data.

I tried using
$scope.datas = JSON.stringify(response);
alert($scope.datas);

It alerts the required data in json formatm, but it is not displayed in show.html page.

how can I proceed . Please help.


#4

What are you using to view your console ?


#5

google chrome console (isn’t that the way to do it?)


#6

It’s wierd that Chrome console shows you [Object], because it would be able to actually decode the object
We’ll try to get you an output.
you could try :

 for(a in array)console.log(array[a])

or

console.dir(functor);

Tell me if you get anything


#7

for(a in $scope.datas)console.log($scope.datas[a]); shows the json array one character per line.

console.dir(functor); -functor is not defined

console.dir($scope.getdata()); -undefined

Tried all these inside http.get success. I am fresher programmer and I don’t know exactly whether I am doing it right


#8

Do you know the format of the response you are expecting ?
Are you sure you receive a valid JSON file ?

If you have an output with

 for(a in $scope.datas)console.log($scope.datas[a])

You might be able to try something else to rebuild the response you are expecting.


#9

I am receiving json formated file as expected.

but I don’t know why I am not able to use ‘datas’ in show.html

Is there any particular reason for controller not sending the value to view?

just for info: If I use .then instead of .success, I get “TypeError: $http.get(…).then(…).error is not a function” in google chrome console.