【发布时间】:2014-09-09 03:25:31
【问题描述】:
我正在尝试将 json 文件加载到网站上,但如果 url 中不存在文件扩展名,则文件无法加载。
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”的情况下添加扩展名变量。
有什么想法吗?
【问题讨论】:
标签: javascript json angularjs angularjs-resource