我有一个角度应用程序,它应该与json-server一起用于检索数据和添加新数据(用户反馈)。所以我有一些数组的json数据库,其中一个是"feedbacks":[]
,目前是空的。在PUT方法我得到:
PUT /feedbacks 404
,这是Chrome控制台PUT http://localhost:3000/feedbacks 404 (Not Found)
。
这是我的服务:
angular.module('myApp')
.constant("baseURL", "http://localhost:3000/")
.service('feedbackService',['$resource','baseURL',function($resource,baseURL){
this.getFeedback=function(){
return $resource(baseURL+"feedbacks/:date",null,{
'update':{
method:'PUT'
}
});
};
}]);
这是控制器:
// contactus.html controllers
.controller('ContactController', ['$scope', function($scope) {
$scope.feedback = {firstName: "",lastName: "",email: "",date: ""};
}])
// Feedback form controller
.controller('FeedbackController', ['$scope', 'feedbackService', function($scope, feedbackService) {
$scope.feedbacks = feedbackService.getFeedback().query(function(response) {
$scope.feedbacks = response;
});
$scope.sendFeedback = function() {
$scope.feedback.date = new Date().toISOString();
$scope.feedbacks.push($scope.feedback);
feedbackService.getFeedback().update($scope.feedbacks);
$scope.feedbackForm.$setPristine();
$scope.feedback = {firstName: "",lastName: "",email: "", date:""};
};
}])
getFeedbacks()
方法工作,服务器发送200,但是对于PUT我收到404.
答案 0 :(得分:0)
好的,我解决了:))一个非常愚蠢的错误。因为我想在数组中创建新对象,所以不需要push然后更新。
$scope.feedback.date = new Date().toISOString();
feedbackService.getFeedback().save($scope.feedback);
我还将服务更改为:
return $resource(baseURL+"feedbacks/:id",null,{
为每个对象设置自动增量ID