处理angularjs中的尾部斜杠

时间:2015-07-21 19:26:41

标签: angularjs

我有一个看起来像这样的函数:

   $scope.deleteTodo = function(id) {
        var url = '/musicians/' + id;
        console.log("URL" + url);
        $http.delete(url, { 'id': id})
            .success(function(data) {
                $scope.todos = data;
                console.log(data);
            })
            .error(function(data) {
                console.log('Error: ' + data);
            });
    };

如果Id为123,我会得到/musicians123

然而,我期待/musicians/123

我直接得到一个错误,它甚至没有打印console.log语句。因为它在$ http.delete之前,我相信它应该已经打印但它没有。

我尝试使用/转义\\/,但没有帮助。

另外,我尝试配置:

.config(['$resourceProvider', function($resourceProvider) {
  // Don't strip trailing slashes from calculated URLs
  $resourceProvider.defaults.stripTrailingSlashes = false;
}]);

堆栈跟踪:

DELETE http://localhost:3000/musicians55accbce27e2cd2802de4894 404 (Not Found)$get.id @ angular.min.js:100n @ angular.min.js:96l @ angular.min.js:95$get.l.(anonymous function) @ angular.min.js:97$scope.deleteTodo @ VM416 core.js:43w @ angular.min.js:73(anonymous function) @ angular.min.js:145$get.e.$eval @ angular.min.js:89$get.e.$apply @ angular.min.js:89(anonymous function) @ angular.min.js:145x.event.dispatch @ jquery.min.js:5x.event.add.y.handle @ jquery.min.js:5
VM416 core.js:49 Error: Error: Not Found
    at Layer.handle (C:\Users\dev4\Desktop\Sample\server.js:86:13)
    at trim_prefix (C:\Users\dev4\Desktop\Sample\node_modules\express\lib\router\index.js:226:17)
    at C:\Users\dev4\Desktop\Sample\node_modules\express\lib\router\index.js:198:9
    at Function.proto.process_params (C:\Users\dev4\Desktop\Sample\node_modules\express\lib\router\index.js:251:12)
    at next (C:\Users\dev4\Desktop\Sample\node_modules\express\lib\router\index.js:189:19)
    at next (C:\Users\dev4\Desktop\Sample\node_modules\express\lib\router\index.js:166:38)
    at next (C:\Users\dev4\Desktop\Sample\node_modules\express\lib\router\index.js:166:38)
    at next (C:\Users\dev4\Desktop\Sample\node_modules\express\lib\router\index.js:166:38)
    at next (C:\Users\dev4\Desktop\Sample\node_modules\express\lib\router\index.js:166:38)
    at next (C:\Users\dev4\Desktop\Sample\node_modules\express\lib\router\index.js:166:38)
    at next (C:\Users\dev4\Desktop\Sample\node_modules\express\lib\router\index.js:166:38)
    at next (C:\Users\dev4\Desktop\Sample\node_modules\express\lib\router\index.js:166:38)
    at next (C:\Users\dev4\Desktop\Sample\node_modules\express\lib\router\index.js:166:38)
    at next (C:\Users\dev4\Desktop\Sample\node_modules\express\lib\router\index.js:166:38)
    at next (C:\Users\dev4\Desktop\Sample\node_modules\express\lib\router\index.js:166:38)
    at next (C:\Users\dev4\Desktop\Sample\node_modules\express\lib\router\index.js:166:38

但没有帮助。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您发布的内容没有任何问题。此外,resourceProvider与如何在字符串中解析斜杠无关。有关工作示例,请参阅http://plnkr.co/edit/Y99E2pQA6RfVRkg1t6zZ?p=preview

angular.module('app', [])
.controller('testCtrl', ['$scope', function($scope) {
  $scope.Demo = function(id) {
        var url = '/base/' + id;
        alert("URL: " + url);  //It gives `base123` if 123 is the id
        };
}]);


<!DOCTYPE html>
<html ng-app="app">

  <head>
    <script data-require="angular.js@1.4.3" data-semver="1.4.3" src="https://code.angularjs.org/1.4.3/angular.js"></script>
    <script src="script.js"></script>
  </head>

  <body ng-controller="testCtrl">
    <a href="" ng-click="Demo('123')">Test Demo Function</a>
  </body>

</html>