AngularJS $ http.post方法不向Grails Action发送数据

时间:2014-10-23 23:17:58

标签: angularjs grails

我的Grails应用程序不接收来自angularjs post请求的数据。

我的AngularJS控制器是:

module.controller('MemberCtrl', ['$scope', '$http',
    function ($scope, $http) {

        $scope.submitForm = function () {
            $http.post(uri + "receiveNewMember", $scope.member)
            .success(function (data) {
                console.log("Data Sent With Success! " + data);
            })
            .error(function (data) {
                console.log("Fail");
            });
        };

    }]);

我的Grails行动是:

def receiveNewMember(){
    render text: params.name
}

调试器停止运行。但是params变量没有任何数据,只有controlleraction

有人知道如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

可以通过Grails Controller中的request.JSON访问发布数据。

答案 1 :(得分:0)

你可以做这样的事情来模仿grails的工作方式: post query parameters like jQuery

我还创建了一个Serializer工厂,将任何类型的javascript对象序列化为grails上的预期方式:

(function() {
    'use strict';
    angular.module('app').factory('Serializer', function ($filter) {
        function SerializerService(){}
        var serializerService = new SerializerService();
        serializerService.excludedProperties = ['$$hashKey'];
        SerializerService.prototype.serialize = function(object){
            var results = {};
            this.serializeObject(results, object, "");
            return results;
        };
        SerializerService.prototype.serializeObject = function(results, object, nameAtTheMoment){
            if($.isArray(object)){
                var array = object;
                for (var i=0; i<object.length; i++){
                    var newNameAtTheMoment = nameAtTheMoment + "[" + i.toString() + "]";
                    this.serializeObject(results, array[i], newNameAtTheMoment)
                }
            }
            else{
                if(Object.prototype.toString.call( object ) === "[object Object]"){
                    var i=0;
                    for(var property in object){
                        if (object.hasOwnProperty(property) && this.excludedProperties.indexOf(property) == -1) {
                            var newNameAtTheMoment;
                            if(nameAtTheMoment !== "")
                                newNameAtTheMoment = nameAtTheMoment + "." + property;
                            else
                                newNameAtTheMoment = property;

                            this.serializeObject(results, object[property], newNameAtTheMoment);
                            i++;
                        }
                    }
                }
                else{ //the object is a simple value
                    if(Object.prototype.toString.call(object) === '[object Date]'){
                        var dateServerFormat = window.appConfig.dateServerFormat;
                        results[nameAtTheMoment] = $filter('date')(object, dateServerFormat);
                    }
                    else
                        results[nameAtTheMoment] = object;
                }
            }
        };
        return serializerService;
    });
})();

按照第一个链接描述,您可以执行以下操作:

angular.module('app', [...])
    .config(function ($provide, $httpProvider) {
        var serializer;
        // Trick to inject dependencies on the config function.
        $provide.factory('FactoryInjector', function () {
            return {
                setSerializer: function(serializerParam){
                    serializer = serializerParam;
                }
            };
        });

        $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
        $httpProvider.defaults.transformRequest = [function(data) {
            return angular.isObject(data) && String(data) !== '[object File]' ? $.param(serializer.serialize(data)) : data;
        }];

    })

    .run(function ($rootScope, $state, $stateParams, Serializer, FactoryInjector) {
        $rootScope.$state = $state;
        $rootScope.$stateParams = $stateParams;

        FactoryInjector.setSerializer(Serializer);
    });