2

this is my service

album.factory('albumService', function($resource) {

    return $resource('/album/v1/album/:id', {id:'@id'})

});

however when I try to create an album

var newAlbum = new albumService($scope.album)
newAlbum.$save()

angular fires a post request to '/album/v1/album', which is wrong , it removes my trailing slash it should be '/album/v1/album/'.

because I'm using tastypie, without the trailing slash an error will rasie

You called this URL via POST, but the URL doesn't end in a slash

where is my trailing slash?

2
  • By default resource strip trailing slashes and set the url Commented Jun 26, 2013 at 6:23
  • then how can I add the trailing slashes Commented Jun 26, 2013 at 6:26

4 Answers 4

2

I used the workaround mentioned in the git issue thread, it works in firefox as well.

angular.module('ngResource').config([
    '$provide', '$httpProvider',
    function($provide, $httpProvider) {
        $provide.decorator('$resource', function($delegate) {
            return function() {
                if (arguments.length > 0) {  // URL
                    arguments[0] = arguments[0].replace(/\/$/, '\\/');
                }

                if (arguments.length > 2) {  // Actions
                    angular.forEach(arguments[2], function(action) {
                        if (action && action.url) {
                            action.url = action.url.replace(/\/$/, '\\/');
                        }
                    });
                }

                return $delegate.apply($delegate, arguments);
            };
        });

        $provide.factory('resourceEnforceSlashInterceptor', function() {
            return {
                request: function(config) {
                    config.url = config.url.replace(/[\/\\]+$/, '/');
                    return config;
                }
            };
        });

        $httpProvider.interceptors.push('resourceEnforceSlashInterceptor');
    }
]);
Sign up to request clarification or add additional context in comments.

1 Comment

Finally an answer that works! Now I can try and get these dents out of the desk caused by me ramming my head into it angrily for the past 2 hours trying to cure this anti-feature.
0

There are probably only two ways to get rid of the problem

1) Adding two forward slash and trailing slash at the end of url \\/(But it will not work in firefox)

2) Use traditional $http

Comments

0

turns out this is a bug for django backend user

Git issue list

Mailing list

1 Comment

This isn't an answer at all.
0

Now (as of Angularjs 1.3.0) you can just turn trailing slashes on: https://docs.angularjs.org/api/ngResource/service/$resource

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.