ngResource($ resource)为PUT返回404

时间:2016-08-04 07:23:04

标签: angularjs ngresource angularjs-ng-resource

我已经有一段时间了,因为我已经使用$ resource来管理我的服务电话。

出于某种原因,我的所有电话都正常工作并达到我的REST端点,基本上是/ api / profile和/ api / profile /:id。

但由于某种原因,我的回报为404。

任何人都知道可能会发生什么。

谢谢,干杯!

'use strict';

angular.module('chainLinkApp')

.config(['$stateProvider', function($stateProvider){
  $stateProvider
  .state('profile', {
    url:'/profile/:id',
    templateUrl:'views/profile.html',
    controller:'ProfileController',
    controllerAs:'profile'
  });
}])

.controller('ProfileController',['$scope', '$http', 'profileFactory', function($scope, $http, profileFactory){


  $scope.updateMode = false;


  $scope.comments = profileFactory.getProfiles.go().$promise.then(function(response){
    $scope.comments = response;
  });


  $scope.getProfile = function(commentId){
    $scope.comment = profileFactory.getProfile.go({id:commentId}).$promise.then(function(response){
      $scope.comment = response;
      $scope.updateMode = true;
    }, function(error){
      return console.log('An error has occured', error);
    });
  };


  $scope.addProfile = function(comment){
    profileFactory.postProfile.go(comment).$promise.then(function(){
      console.log('Your post was a success');
      $scope.comment = {};
    }, function(error){
      console.log('There was an error: ', error);
    });
  };


  $scope.updateProfile = function(comment){
    profileFactory.updateProfile.go(comment._id, comment).$promise.then(function(response){
      console.log('Your profile has been updated');
      $scope.updateMode = false;
      $scope.comment = {};
    }, function(error){
      console.log('There is an error: ', error);
    });
  };
}])


.factory('profileFactory', ['$resource', function($resource){

  return{
    getProfiles:    $resource('/api/profile', {}, { go: { method:'GET', isArray: true }}),
    getProfile:     $resource('/api/profile/:id',{},{ go: { method: 'GET', params: { id: '@id' }}}),
    postProfile:    $resource('/api/profile', {}, { go: { method: 'POST' }}),
    updateProfile:  $resource('/api/profile/:id', {}, { go: { method: 'PUT', params: { id:'@id' }}})
  };

}]);

1 个答案:

答案 0 :(得分:0)

你使用$resource的方式很奇怪,它应该是这样的:

.factory('UserService', function($resource) {
    return $resource('/api/users/:id', {}, {
        'create': { method: 'POST' },
        'update': { method: 'PUT', params: { id: '@id'} }

    });
})

然后你就像这样调用服务:UserService.create(theUserobj, function(result) { ... })