rest - AngularJS - PUT method not working (404 error) -


i'm new angularjs , have trouble update object via rest. i'm using php/mysql backend (slim framework).
i'm able retrieve (get), create (post) new object not edit (put) one. here's code:

my form:

<form name="actionform" novalidate ng-submit="submitaction();">   name: <input type="text" ng-model="action.name" name="name" required>   <input type="submit"> </form> 

my service:

var appservices = angular.module('appservices', ['ngresource']) appservices.factory('appfactory', function($resource) {   return $resource('/api/main/actions/:actionid', {}, {     'update': { method: 'put'},   }); }); 

app.js

var app = angular.module('app', ['appservices']) app.config(function($routeprovider) {   $routeprovider.when('/main/actions', {     templateurl: 'partials/main.html',     controller: 'actionlistctrl'   });   $routeprovider.when('/main/actions/:actionid', {     templateurl: 'partials/main.html',     controller: 'actiondetailctrl'   });   $routeprovider.otherwise({redirectto: '/main/actions'}); }); 

controllers.js:

function actiondetailctrl($scope, $routeparams, appfactory, $location) {   $scope.action = appfactory.get({actionid: $routeparams.actionid});   $scope.addaction = function() {     $location.path("/main/actions/new");   }   $scope.submitaction = function() {     // update case     if ($scope.action.actionid > 0) {       $scope.action = appfactory.update($scope.action);       alert('action "' + $scope.action.title + '" updated');     } else {       // create case       $scope.action = appfactory.save($scope.action);       alert('action "' + $scope.action.title + '" created');     }     $location.path("/main/actions");   } } 

in slim, in api/index.php, i've got these routes , functions defined:

$app->get('/main/actions', 'getactions'); $app->get('/main/actions/:actionid',    'getaction'); $app->post('/main/actions', 'addaction'); $app->put('/main/actions/:actionid', 'updateaction'); 

when create new "action", working expected. when try edit existing one, i've got error:

put http://project.local/api/main/actions 404 not found

the action not updated (although alert message "action xxx updated" displayed)

is issue routeprovider setting ? guess put url misses id @ end...

i precise if try simulate put request postman-chrome-extension example, working (put http://project.local/api/main/actions/3 returns expected data)

after reading more carefully, problem indeed don't send id in url when update request. furthermore, way call update / save not quite intended resource. after have created resource, call save / update methods on instance, not factory function. can bind properties of instance params in url, don't have specify them on each call:

appservices.factory('appfactory', function($resource) {   return $resource('/api/main/actions/:actionid', {actionid : '@actionid'}, {     'update': { method: 'put'}, }); 

the second parameter of resource tells angular properties should use populate url parameters. operation stays same:

$scope.action = appfactory.get({actionid: $routeparams.actionid}); 

and update now:

$scope.submitaction = function() {   // update case   if ($scope.action.actionid > 0) {     $scope.action.$update()   } else {     $scope.action.$save();     } } 

since used angular-cellar basis, should check , see if can improve ...


Comments