【问题标题】:Failing to load resource without extension无法加载没有扩展名的资源
【发布时间】: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


    【解决方案1】:

    你可以在:id后面输入.json,检查:https://docs.angularjs.org/api/ngResource/service/$resource#usage

    【讨论】:

    • 感谢您的参考!
    猜你喜欢
    • 2012-05-07
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    • 2021-05-14
    • 2012-07-16
    • 2019-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多