通过JSON发送请求参数

时间:2016-03-04 15:27:22

标签: angularjs json spring-mvc

我对Angular和Spring MVC都很新。我期待着你的帮助。

这是我的控制器:

@RequestMapping(value ="/PostFormData", method = RequestMethod.POST)
    public @ResponseBody User PostService(@RequestBody User user) {
        System.out.println(user.getFirstName());
        return user;
    }

我的Js文件(我实际上正在使用AngularJS):

var app = angular.module('MyApp', ['ngResource', 'ngRoute']);
    app.config(function ($httpProvider) {
    $httpProvider.defaults.transformRequest = function(data){
        if (data === undefined) {
            return data;
        }
        return $.param(data);
    }
});
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
app.config(function($routeProvider){
        $routeProvider
    .when('/',{
         templateUrl: 'index.html'
        }).when('/profile',{
        templateUrl: '../pages/profile.html'
    });
 });
    app.controller('FormSubmitController',[ '$scope', '$http', function($scope, $http) {
    $scope.list = [];
        $scope.headerText = 'AngularJS Post Form Spring MVC example: Submit below form';
        $scope.submit = function() {
                var formData = {
                    "firstName" : $scope.firstName,
                    "lastName" : $scope.lastName,
                    "login" : $scope.login,
                    "password":$scope.password,
                    "role" :$scope.role
            };
            var response = $http.post('PostFormData',formData);
            response.success(function(data, status, headers, config) {
                $scope.list.push(data);
            });
    }]);

0 个答案:

没有答案