简体   繁体   English

ngResource($ resource)为PUT返回404

[英]ngResource ($resource) is returning 404 for PUT

It's been a while since I've used $resource for managing my service calls. 自从我使用$ resource管理服务调用已经过去了一段时间。

For some reason, all my calls are working ok and reaching my REST end-points, basically /api/profile and /api/profile/:id. 出于某种原因,我所有的调用都可以正常进行,并达到了我的REST端点,基本上是/ api / profile和/ api / profile /:id。

But for some reason, my put returns as 404. 但是由于某种原因,我的看跌期权回报为404。

Anyone have an Idea of what may be going on. 任何人都有可能发生的事情的想法。

Thanks and Cheers! 谢谢,干杯!

'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' }}})
  };

}]);

The way of you are using $resource is strange, it should be like this: 您使用$resource很奇怪,应该像这样:

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

    });
})

Then you call the service like this: UserService.create(theUserobj, function(result) { ... }) 然后,您像这样调用服务: UserService.create(theUserobj, function(result) { ... })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM