打开json文件的错误

时间:2014-11-10 01:09:37

标签: javascript json angularjs

晚上好!我正在学习angular.js,目前我正试图访问json文件以从服务器加载一些数据,但是,不幸的是,$ http - 方法给出了错误。请?帮我找错。

发送查询的控制器代码:

(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文件正常(在解析器中检查)。谢谢!

2 个答案:

答案 0 :(得分:1)

似乎http://patutinskiy.esy.es/服务器不允许交叉来源请求。如果在上面的脚本上运行时启用了javascript debug,则会抛出以下错误:

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. 

如果您具有上述服务器的访问权限,则必须添加标题' Access-Control-Allow-Origin'。阅读这些更多细节:

答案 1 :(得分:0)

在编码json之前发送标题。

"Content-type: application/json" 

你如何编码到json?