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?

4

4 回答 4

2

我使用了 git issue 线程中提到的解决方法,它也适用于 firefox。

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');
    }
]);
于 2014-07-03T14:34:54.733 回答
0

现在(从 Angularjs 1.3.0 开始)你可以打开斜杠:https ://docs.angularjs.org/api/ngResource/service/ $resource

于 2014-08-23T05:26:09.543 回答
0

turns out this is a bug for django backend user

Git issue list

Mailing list

于 2013-06-26T07:11:10.307 回答
0

可能只有两种方法可以解决这个问题

1)在url末尾添加两个正斜杠和斜杠\\/(但在firefox中不起作用)

2) 使用传统的 $http

于 2013-06-26T07:05:17.990 回答