无法从本地服务器上的 URL 读取 AngularJS $http 中的 JSON
Unable to read JSON in AngularJS $http from URL on local server
我正在开发我的第一个 AngularJS 应用程序,对于我的数据,我正在尝试从本地计算机上的 Drupal 网站的页面中读取。它在 Apache 上是 运行,可以通过别名 URL(即 http://mylocalsite instead of http://localhost/mylocalsite)访问。该页面仅显示 JSON 数据数组,但由于某种原因,我的 Angular 应用无法使用 $http 读取它,或者
angular.module('nbd7AppApp')
.controller('BlogListCtrl', ['$scope', '$http', function ($scope, $http) {
$http.get('http://mylocalsite/blogs/json'})
.success(function(data) {
$scope.nodes = data;
});
}]);
或
angular.module('nbd7AppApp')
.controller('BlogListCtrl', ['$scope', '$http', function ($scope, $http) {
$http({method: 'GET', url: 'http://mylocalsite/blogs/json'})
.success(function(data) {
$scope.nodes = data;
});
}]);
但是,如果我将 JSON 放入本地文件并像这样访问它
angular.module('nbd7AppApp')
.controller('BlogListCtrl', ['$scope', '$http', function ($scope, $http) {
$http.get('views/blogs.json')
.success(function(data) {
$scope.nodes = data;
});
}]);
它工作得很好。我是否需要做一些不同的事情才能从网站上阅读它 URL?
谢谢。
如我上面的评论所示,答案是 CORS。将此行添加到我的 Apache 虚拟主机后:
Header set Access-Control-Allow-Origin "*"
错误消失了。
我正在开发我的第一个 AngularJS 应用程序,对于我的数据,我正在尝试从本地计算机上的 Drupal 网站的页面中读取。它在 Apache 上是 运行,可以通过别名 URL(即 http://mylocalsite instead of http://localhost/mylocalsite)访问。该页面仅显示 JSON 数据数组,但由于某种原因,我的 Angular 应用无法使用 $http 读取它,或者
angular.module('nbd7AppApp')
.controller('BlogListCtrl', ['$scope', '$http', function ($scope, $http) {
$http.get('http://mylocalsite/blogs/json'})
.success(function(data) {
$scope.nodes = data;
});
}]);
或
angular.module('nbd7AppApp')
.controller('BlogListCtrl', ['$scope', '$http', function ($scope, $http) {
$http({method: 'GET', url: 'http://mylocalsite/blogs/json'})
.success(function(data) {
$scope.nodes = data;
});
}]);
但是,如果我将 JSON 放入本地文件并像这样访问它
angular.module('nbd7AppApp')
.controller('BlogListCtrl', ['$scope', '$http', function ($scope, $http) {
$http.get('views/blogs.json')
.success(function(data) {
$scope.nodes = data;
});
}]);
它工作得很好。我是否需要做一些不同的事情才能从网站上阅读它 URL?
谢谢。
如我上面的评论所示,答案是 CORS。将此行添加到我的 Apache 虚拟主机后:
Header set Access-Control-Allow-Origin "*"
错误消失了。