通过 $http 发布对象

Posting Objects via $http

你好,我有点困惑,当我 post 我的表单数据没有发送到服务器时,我检查了我的控制台好几次,但我不明白发生了什么,这是我的控制器:

angular.module('app').controller('InstCtrl', ['$http', '$scope', '$state', 'InstService', function($http, $scope, $state, InstService) {

    var parameters = {
        name: $scope.name,
        description: $scope.description,
        email: $scope.email,
        phone: $scope.phone,
        website: $scope.website,
        isActive: $scope.isActive
    };

    $scope.addInstitution = function(parameters) {

        InstService.add(parameters);
        console.log(parameters);

    };

}]);

这是服务:

angular.module('app').factory('InstService', function ($http) {

    var InstService = {};

    InstService.add = function(parameters) {

        return $http
            .post('/api/v1/institutions/', parameters)

    };

    return InstService;
});

我在前端使用 AngularJS,在我的服务器上使用 laravel 5。

console.log returns 未定义 我的 $http.post 总是空的,因为没有数据被 post 发送到服务器。

您实际在哪里调用 addInstitution 方法?假设它来自您 html 中的按钮点击。如果是这样,我认为这行不通。在设置这些范围变量之前定义参数对象。

尝试将参数定义移动到 addInstitution:

$scope.addInstitution = function() {
   var parameters = {
       name: $scope.name,
       description: $scope.description,
       email: $scope.email,
       phone: $scope.phone,
       website: $scope.website,
       isActive: $scope.isActive
    };

    console.log(parameters);

    InstService.add(parameters);
};