-5

I am having trouble to read data from my test.json file with the #http service. I am running all in a xampp localhost, can't really figure what is happening, here is the js code. Thanks in advance for the help.

<script type="text/javascript">
var app = angular.module('myApp', []);
        app.controller('myCtrl', ['$http', '$scope', function($http, $scope){
            $http.get('test.json').success(function(response){
                $scope.myData = response;
            });
        }]);
</script>

EDIT: That is the json file:
[{"id":"1","name":"John"}, {"id":"2","name":"Paul"},]

And this is the rest of the html:

<ul>
    <li ng-repeat="data in myData">
        {{data.id}}
        {{data.name}}
    </li>
</ul>
5
  • Have you got error in your console ? Commented May 20, 2016 at 16:09
  • Check you js console and tell us what is error or msg is printing there. Commented May 20, 2016 at 16:09
  • Where is the test.json? Are you parsing the response which is json? Commented May 20, 2016 at 16:12
  • this code should not make any trouble. please post your full code. Commented May 20, 2016 at 16:29
  • I've got no errors showing up in the console. I've got two files in a folder one is the test.json and the other is the index.php file. Commented May 21, 2016 at 11:05

2 Answers 2

-1

The $http legacy promise methods success and error have been deprecated. Use the standard then method instead.

JAVASCRIPT

<script type="text/javascript">
var app = angular.module('myApp', []);
        app.controller('myCtrl', ['$http', '$scope', function($http, $scope){
            $http.get('test.json').then(function(response){
                $scope.myData = response;
            });
        }]);
</script>
Sign up to request clarification or add additional context in comments.

Comments

-1

Try and format your data before passing to scope with the angular.fromJson api.

try this

$scope.myData = angular.fromJson(response);

instead of

$scope.myData = response;

and remember you should resolve with .then not success hope it helps.

2 Comments

Just tried using angular.fromJson, and now the console shows a 404 error.
Never mind I had the wrong name that's why the 404 happend, but still got the same problem. No errors in the console too.

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.