Dan Jimenez
Dan Jimenez

Reputation: 115

Failing to load resource without extension

I'm trying to load a json file onto a site but the file fails to load if the file extension is not present in the 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;
        }
    };
});

In the previous code, if I take out the ".json" the file fails to load. This would normally not be a big deal, except I am trying to learn how to use the $resource object in AngularJS and thus am getting an error since I can't specify file extension when using the object.

So if I try to use this:

    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);
        }
    };
});

The JSON file will fail to load and am not quite sure if there is a way to define extension without affecting the ":id" variable.

So, two ways I'm thinking I could solve this. Either a) I might need to fix something in my system to be able to load files without defining extension (meaning there is a system config error) or b) there is a way with JavaScript to add an extension without affecting the ":id" variable.

Any thoughts?

Upvotes: 0

Views: 208

Answers (1)

Pepijn
Pepijn

Reputation: 1204

You can just enter .json after :id, check: https://docs.angularjs.org/api/ngResource/service/$resource#usage

Upvotes: 1

Related Questions