3

How can I get json data via ajax with angular? I tried a lot but my code is not working. My code:

<!DOCTYPE html>
<html lang="en" ng-app="test">

<head>
  <meta charset="UTF-8">
  <title></title>
  <script src="https://code.angularjs.org/1.6.1/angular.min.js"></script>
  <script>
    angular.module('test', [])
      .controller('TestCtrl', function($scope, $http) {
        $scope.data = [];
        $http.get("http://jsonplaceholder.typicode.com/users").then(function(res) {
          $scope.data = JSON.parse(res);
        });
      });
  </script>
</head>

<body ng-controller="TestCtrl">
  <div ng-repeat="item in data">
    {{item.id}}
  </div>
</body>

</html>
1
  • would you please explain what happens with your code? Does it throw an error in the browser console? Commented Jan 17, 2017 at 2:25

1 Answer 1

2

Did you try like this..

<script>
        var app = angular.module('test', [])
        app.controller('TestCtrl', function ($scope, $http) {
            $http.get("http://jsonplaceholder.typicode.com/users").then(function(res,status,xhr) {
                $scope.data = res.data;
            });
        });
    </script>
Sign up to request clarification or add additional context in comments.

1 Comment

@Enes Giray glad to help you.Best of luck ahead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.