post [object%20Object] 404 not found angularjs

CoolLife picture CoolLife · Dec 8, 2015 · Viewed 12k times · Source

hi i am trying to insert a row in mi table but always obtain this problem:

POST URL/api/presentaciones/[object%20Object] 404 (Not Found)

Well I have a form and the ng-submit="savePresentacion" inthe controller:

.controller('newPresentacionController', function ($scope, Presentacion, Categoria) {
             $scope.categorias ={};
             var vm = this;
             var presentacionData = {};
             $scope.savePresentacion = function() {
                 presentacionData = {
                     nombre : $scope.nombre,
                     descripcion : $scope.descripcion,
                     tipo_presentacion : $scope.tipo_presentacion,
                     usuarios_id : $scope.usuarios_id,
                     categorias_id: $scope.categorias_id,
                     longitude: self.myMarker.position.lng(),
                     latitud: self.myMarker.position.lat(),
                     zoom: 13
                 };
                 Presentacion.crearPresentacion(presentacionData).success(function (datos) {

                 });
             };
}

in the presentacionService have this:

(function () {
    angular.module('presentacionService', [])
        .factory('Presentacion', function ($http) {
            var presentacionFactory = {};
            presentacionFactory.crearPresentacion = function(presenData) {
                console.log(presenData);
                return $http.post('/api/presentaciones/' + presenData);
            };
            return presentacionFactory;
        });
})();

after of this give the problem. I did the same with 2 tables more and dont have problem just have problem with this part.

Answer

Roddy of the Frozen Peas picture Roddy of the Frozen Peas · Dec 8, 2015

[object Object] is how a JavaScript object is rendered by default. Your console.log(presenData) will similarly output [object Object].

Given that you define presenData as a JSON object, I'm guessing you want to pass that in its entirety in your POST.

Change this:

return $http.post('/api/presentaciones/' + presenData);

to this:

return $http.post('/api/presentaciones/', presenData);

Note the comma. This will pass the presenData JSON object as the body of the request.

I would also change the logging statement to be console.log(JSON.stringify(presenData)), which will render the JSON representation to the console.