使用路由参数在 API PUT 调用中出现 404 错误
Getting 404 error on API PUT call with route param
这是 API url 我想在我的 Express 应用程序中点击:
// Dashboard API to update account
app.post('/api/accounts/:id', accountsController.update);
我的完整账户模块有Accounts factory,下面具体描述:
(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)
你有app.post('/api/accounts/:id', accountsController.update);
应该是app.put
这是 API url 我想在我的 Express 应用程序中点击:
// Dashboard API to update account
app.post('/api/accounts/:id', accountsController.update);
我的完整账户模块有Accounts factory,下面具体描述:
(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)
你有app.post('/api/accounts/:id', accountsController.update);
应该是app.put