【问题标题】:Getting 404 error on API PUT call with route param使用路由参数调用 API PUT 时出现 404 错误
【发布时间】:2015-03-11 18:22:59
【问题描述】:

这是我试图在我的 Express 应用中访问的 API 网址:

// Dashboard API to update account
app.post('/api/accounts/:id', accountsController.update);

我的完整帐户模块与帐户工厂,然后是下面的具体描述:

(function() {

    var app = angular.module('app-accounts',
    ['ngAnimate', 'ngResource', 'account-directives'])

    .controller('AcctCtrl',
        ['$scope', '$resource', 'Accounts',
        function($scope, $resource, Accounts) {

        var vm = $scope;
            vm.$parent.modal = false;

        var Account = $resource('/api/accounts');

        // Open the edit account modal:
        this.editAccount = function(id, label, address) {
            console.log(id);
            vm.dash.modal = true;
            Accounts.modalEditAccount(vm.dash, id, label, address);
        };

        vm.dash.updateAccount = function(i) {
            console.log(i);
            // Call Update method from Accounts factory
            Accounts.update(i, $scope.new_label, $scope.new_address);
        }
    }])

    // Accounts factory (open edit model, get all, update, remove):
    .factory('Accounts', ['$http', '$resource', function($http, $resource) {

        var accountsFactory = {};

        accountsFactory.modalEditAccount = function(vm, id, label, address) {
            vm.modal_edit_account = true;
            vm.acct_id = id;
            vm.acct_label = label;
            vm.acct_address = address;
            vm.save_btn_text = 'save';
        };

        // Get all the accounts
        accountsFactory.all = function() {
            return $http.get('/api/stuff');
        };

        // Updates an account
        accountsFactory.update = function(id) {
            return $http.put('/api/accounts/'+id);
        };

        // Delete account
        accountsFactory.remove = function(id) {
            return $http.delete('/api/accounts/'+id);
        };

        return accountsFactory;

    }]);

})();

updateAccounts函数获取选中账户的id,并传入Accounts工厂中的更新函数:

vm.dash.updateAccount = function(i) {
    console.log(i);
    // Call Update method from Accounts factory
    Accounts.update(i, $scope.new_label, $scope.new_address);
}

接下来在我的Accounts 工厂中,这里是 PUT/UPDATE 方法:

// Updates an account
accountsFactory.update = function(id) {
    return $http.put('/api/accounts/'+id);
};

// ^ call is to "/api/accounts/acct-1"

然后是我的 Express API 更新路线:

// Dashboard API to update account
app.post('/api/accounts/:id', accountsController.update);

最后是我在服务器上的accounts-controller.j

module.exports = {
    create: function(req, res) {
        console.log(req.body);
    },

    update: function(req, res) {
        console.log(req.body);
    }
};

对我为什么得到 404 有任何想法吗?
PUT http://localhost:9999/api/accounts/acct-2 404 (Not Found)

【问题讨论】:

    标签: angularjs node.js express mean-stack angular-http


    【解决方案1】:

    你有app.post('/api/accounts/:id', accountsController.update);

    应该是app.put

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-26
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      • 2015-04-10
      • 2021-05-04
      • 2014-11-12
      • 1970-01-01
      相关资源
      最近更新 更多