Sheetal Jain Sheetal Jain - 1 year ago 114
JSON Question

Unable to show webservice response in html using angularjs

I am fetching a list of users from angular js by making a call to spring rest webservice . I am receiving a response in my service but not able to print the same in html file. There is no error on console.


var deferred=$q.defer();
var url="http://localhost:8081/HCP_Android_Demo/fetchUsers";
return ${
alert("The error response is"+response);
return deferred.promise;


function fetchUsers(){
$scope.userData =;
$scope.result =;
alert("users list is"+$scope.userData);//prints [Object object]
alert("result is"+$scope.result);//prints ok

public String fetchUsers()throws JSONException{
System.out.println("Inside fetchUsers");
List<User> userList=userService.fetchUsers();
ArrayList<JSONObject> arrayList=new ArrayList<JSONObject> ();
for(User user:userList){
JSONObject jsonObject=new JSONObject();
jsonObject.put("name", user.getUsername());
jsonObject.put("address", user.getAddress());
catch(Exception exception){

JSONObject usersList=new JSONObject();
usersList.put("Result", "OK");
usersList.accumulate("usersList", arrayList);
/*return callBackFunction +"(" + jsonObject.toString() + ")"; */
return usersList.toString() ;



<body ng-app="myApp" ng-controller="UserController as ctrl">
<span class="tableLabel">List Of users</span>
<tr ng-repeat="users in ctrl.userData ">



Answer Source

You are using controller as syntax witch do you create a new instance and you can access to properties you have bound to controller directly from the controller instance. If you want to use controller as syntax then you bind your function or object to the controller instead $scope. You could do this

var vm = this.

vm.yourData = resultFromBeckend ;

and in view

ng-controller="Ctrl as ctrl"

In your case change this line. From

 $scope.userData =;


 vm.userData =;

Your userData is not on the controller it's on the $scope. It's also best practice to bind your object to the controller and use controller as syntax. Angular Best Practice from JhonPapa

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download