Arun Arun - 3 months ago 7
Ajax Question

Angularjs displaying ajax response

Basically my angular code looks like this

var myapp = angular.module("Demo",["ngRoute"])
.config(function($routeProvider){
$routeProvider
.when("/students",{
templateUrl : "templates/students.html",
controller : "grouplistcontroller"
})
})
.controller("grouplistcontroller", function($scope,$http){
$http.get("ajaxfunction.php?action=getlist")
.then(function(res) {
var obj = JSON.stringify(res);
console.log(obj);
$scope.students = obj.data;
});
});


The json data from server looks like this

"data":[
{
"sub_id":"1199",
"sub_group":"GT"
},
{
"sub_id":"727",
"sub_group":"GT"
},
{
"sub_id":"660",
"sub_group":"GT"
},
{
"sub_id":"614",
"sub_group":"GT"
}
],
"status":200,
"config":{
"method":"GET",
"transformRequest":[
null
],
"transformResponse":[
null
],
"url":"ajaxfunction.php?action=getlist",
"headers":{
"Accept":"application/json, text/plain, */*"
}
},
"statusText":"OK"


I am trying to display the loop from the result in my front end, which isnt working

<ul>
<li ng-repeat="student in students">
{{ student.sub_id }}
</li>
</ul>

Answer

Hi You Don't have to convert JSON.stringify(obj), Simply Use as Object ... Use the Below Controller

.controller("grouplistcontroller", function($scope,$http){                      
                $http.get("ajaxfunction.php?action=getlist")
                    .then(function(res) {   


                        $scope.students = res.data;
                    });                     
            });
Comments