post [object%20Object] 404 未找到 angularjs

post [object%20Object] 404 not found angularjs

您好,我正在尝试在 mi table 中插入一行,但总是遇到这个问题:

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

我有一个表单和控制器中的 ng-submit="savePresentacion":

.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) {

                 });
             };
}

在 presentacionService 中有这个:

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

在此之后给出问题。我对 2 tables 做了同样的事情,没有问题只是这部分有问题。

你的问题是你的 presenData 是一个对象。当您将它附加到这样的字符串时: '/api/presentaciones/' + presenData

Javascript是将对象转为字符串,用[object Object]表示。

您需要确定 presenData 上的哪个属性代表您尝试访问的端点。例如,如果 presenData 有一个 .id,您可能希望在字符串后附加 .id 参数。

[object Object] 是 JavaScript 对象默认的渲染方式。您的 console.log(presenData) 将类似地输出 [object Object].

鉴于您将 presenData 定义为 JSON 对象,我猜您想在 POST.

中完整传递它

改变这个:

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

对此:

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

注意逗号。这会将 presenData JSON 对象作为请求的主体传递。

我还将日志记录语句更改为 console.log(JSON.stringify(presenData)),这会将 JSON 表示形式呈现给控制台。