javascript - 如何在本地运行平均堆栈应用程序时获取包含在 angularjs 控制器中的 json 文件?
问题描述
我正在尝试在本地运行的平均堆栈应用程序中包含一个带有数据的 json 文件。
这些方法我都试过了,还是报404错误
$http.get('MOCK_DATA.json').success(function (res){
$scope.Alldata = res.data;
$scope.$apply;
});
var getPromise = $http.get('MOCK_DATA.json').then(function(response){
//whatever is returned here will be accessible in the .then that is chained
return response.data;
});
console.log(getPromise);
$http({
method: 'get',
url: 'MOCK_DATA.json'
}).then(function (response){
$scope.Alldata = response;
},function (error){
});
解决方案
只需要从正确的文件中导入 json 文件,你就可以像 var data = require('./data.json');