简体   繁体   English

打开json文件时的错误

[英]The mistake in opening a json-file

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. 我正在学习angular.js,目前正在尝试访问json文件以从服务器加载一些数据,但是,不幸的是,$ http-方法给出了错误。 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). Json文件正常(在解析器中检查)。 Thank you! 谢谢!

It seems that the http://patutinskiy.esy.es/ server doesn't allow cross origin request. 似乎http://patutinskiy.esy.es/服务器不允许跨源请求。 If you enable javascript debug when running above script it will throw following error: 如果您在运行上述脚本时启用了JavaScript调试,则会引发以下错误:

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'. 如果您具有提到的服务器的访问权限,则必须添加标题“ Access-Control-Allow-Origin”。 Read these for more detail: 阅读这些以获取更多详细信息:

Send header before encode json. 在编码json之前发送标头。

"Content-type: application/json" 

And how u encode to json? 以及如何编码为json?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM