0

Good evening! I'm learning angular.js and at the moment I'm trying to get access to the json-file to load some data from the server, but, ubfortunately, $http - method gives an error. Please? help me to find a mistake.

A code of the controller that sends a query:

(function(){
var app =angular.module('SCBI', []) ;
app.controller('TeachersController', function TeachersController ($scope, $http){

$http({method: 'GET', url: 'http://patutinskiy.esy.es/javascripts/json/all_teachers.json'}).
         success(function(data, status, headers, config) {
            $scope.teachers=data;
        }).
        error(function(data, status, headers, config) {
            alert(status);

});
});
})(); 

Json-file is OK (checked in the parser). Thank you!

2 Answers2

1

It seems that the http://patutinskiy.esy.es/ server doesn't allow cross origin request. If you enable javascript debug when running above script it will throw following error:

XMLHttpRequest cannot load http://patutinskiy.esy.es/javascripts/json/all_teachers.json. 
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin    
'xxx' is therefore not allowed access. 

If you have the access of mentioned server then you have to add header 'Access-Control-Allow-Origin'. Read these for more detail:

Community
  • 1
  • 1
Jon Kartago Lamida
  • 827
  • 1
  • 7
  • 12
0

Send header before encode json.

"Content-type: application/json" 

And how u encode to json?

Vitalii Kempa
  • 23
  • 1
  • 4