0

I create some json file which I try to access with my angular.

My JSON looks like:

[
  {"name": "China", "population": 12313214314},
  {"name": "USA", "population": 97888945},
  {"name": "Germany", "population": 456789},
  {"name": "Uzbekistan", "population": 18877},
  {"name": "Tadžikistan", "population": 499999},
  {"name": "Turkistan", "population": 111111}
]

So really simple list of object where is just name of the country and some imaginary population.

I create a controller on it which loooks like:

// Somewhere on a top is this line
var app = angular.module('myApp', []);
// And controller here
        app.controller('CountryJsonCtrl', function($scope, $http){
            $http.get('countries.json').success(function(data){
                $scope.counties = data;
            })
        })

In a HTML it looks like:

<div ng-controller="CountryJsonCtrl">
    <table>
        <tr>
            <th>Country</th>
            <th>Population</th>
        </tr>
        <tr ng-repeat="country in countries">
            <td>{{country.name}}</td>
            <td>{{country.population}}</td>
        </tr>
    </table>
</div>

So I create a table and each row should be in new line via ng-repeat. Dispite this fact countries are not loaded in APP so I look to browser console where I get this error:

XMLHttpRequest cannot load file:///C:/AngularJS/countries.json. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

Can someboy help me where is a problem with this or explain e what I am doing wrong? Beause I am just beginer in Angular I appriciate any advise


EDIT: After advise of @Jon Snow I run this on localhost, result is there is no more error in console , and I see json was loaded in network tab but still no result on a site.

1 Answer 1

2

You're not allowed to do HTTP requests because you're opening the HTML file directly. You need set up a local server.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for adivse, is something like XAMPP with apache enaugh for it? Or do I need something else?
Yeah XAMPP/WAMP should do the job just fine.
Cool, I just run it on localhost, @Jon Snow may I ask you to check my edit?
Its kinda hard to know why its not working just by looking at it, but can you log the data in the console?
just find it out, missspell in controller and in HTML
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.