我正在尝试将json文件加载到网站上,但如果文件扩展名不在网址中,则文件无法加载。
eventsApp.factory('eventData', function($http, $q) {
return {
getEvent: function() {
var deferred = $q.defer();
$http({method: 'GET', url: 'data/event/1.json'}).
success(function(data, status, headers, config) {
deferred.resolve(data);
}).
error(function(data, status, headers, config) {
deferred.reject(status);
});
return deferred.promise;
}
};
});
在前面的代码中,如果我取出“.json”,则文件无法加载。这通常不是什么大问题,除了我试图学习如何在AngularJS中使用$ resource对象,因此在使用该对象时我无法指定文件扩展名,因此收到错误。
所以,如果我尝试使用它:
eventsApp.factory('eventData', function($resource) {
var resource = $resource('/data/event/:id', {id: '@id'}, {"getAll": {method: "GET", isArray:true, params: {something: "foo"}}});
return {
getEvent: function() {
return resource.get({id:1});
},
save: function(event) {
event.id = 999;
return resource.save(event);
}
};
});
JSON文件无法加载,并且不太确定是否有办法定义扩展而不影响“:id”变量。
所以,我认为有两种方法可以解决这个问题。要么a)我可能需要在我的系统中修复一些东西才能加载文件而不定义扩展(意味着存在系统配置错误)或b)有一种方法可以用JavaScript添加扩展而不影响“:id”变量
有什么想法吗?
答案 0 :(得分:1)
您可以在以下后输入.json:id,check:https://docs.angularjs.org/api/ngResource/service/$resource#usage