可能未处理的拒绝:-尝试将数据从angularJS传递到spring MVC时

时间:2018-09-24 10:55:04

标签: javascript angularjs spring-mvc

此错误使我经受了很多考验。有人可以帮我做错什么吗?

我有一个脚本,其中包含JSON格式的几个参数的值。

var mpdApp = angular.module('mpdApp', ['ngAnimate',
    'ngSanitize', 'ui.bootstrap'
]);

mpdApp
    .controller(
        'mpdController',
        [
            '$scope',
            '$rootScope',
            '$http',
            '$window',

            function($scope, $rootScope, $http, $window) {
                $scope.doValidation = function() {

                    var mpdData = {
                        'mpdPolicyNumber': $scope.mpdPolicyNumber,
                        'mpdFirstName': $scope.mpdFirstName,
                        'mpdLastName': $scope.mpdLastName,
                        'mpdAddress': $scope.mpdAddress,
                        'mpdPhoneNumber': $scope.mpdPhoneNumber
                    };

                    console.log(mpdData);

                    $http({
                            'url': '/mpdValidate',
                            'method': 'POST',
                            'headers': {
                                'Content-Type': 'application/json'
                            },
                            'params': mpdData
                        })
                        .then(
                            function(response) {
                                $rootScope.resultMpdData = response.data;
                            });
                };
            }
        ]);

当我尝试通过POST将其传递给spring mvc控制器时,

spring mvc控制器方法

@PostMapping(path = "/mpdValidate", produces = "application/json")
    @ResponseBody
    public List<MPDValidationOutputVO> mpdValidate(@RequestParam("mpdPolicyNumber") String mpdPolicyNumber,
            @RequestParam("mpdFirstName") String mpdFirstName, @RequestParam("mpdLastName") String mpdLastName,
            @RequestParam("mpdAddress") String mpdAddress, @RequestParam("mpdPhoneNumber") String mpdPhoneNumber)
            throws JsonParseException, JsonMappingException, IOException {

        System.out.println("*************");
        MPDValidationInputVO mpdValidationInput = new MPDValidationInputVO();
        List<MPDValidationOutputVO> mpdValidationOutput = mpdValidationService.mpdValidate(mpdValidationInput);
        return mpdValidationOutput;
    }

我收到此错误。 error

控制台日志

{mpdPolicyNumber: "werwer", mpdFirstName: "czxc", mpdLastName: "werwe", mpdAddress: "ghhdf", mpdPhoneNumber: "asaa"}

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

尝试使用此版本

$http({
        url: 'request-url',
        method: "POST",
        data: { 'message' : message }
    })

由于url已经是一个参数,因此请勿将其用单引号引起来。

答案 1 :(得分:0)

produces =“ application / json”仅在Accept标头为“ application / json”时才能处理

您的方法中的参数过多,仅缺少1个参数,将导致404。

如果参数不可见,则可能要添加默认值或required = false。它可以帮助您防止404错误。

@PostMapping(path = "/mpdValidate", produces = "application/json")
    @ResponseBody
    public List<MPDValidationOutputVO> mpdValidate(@RequestParam("mpdPolicyNumber") String mpdPolicyNumber,
            @RequestParam("mpdFirstName") String mpdFirstName, @RequestParam("mpdLastName") String mpdLastName,
            @RequestParam("mpdAddress") String mpdAddress, @RequestParam("mpdPhoneNumber") String mpdPhoneNumber)
            throws JsonParseException, JsonMappingException, IOException {

        System.out.println("*************");
        MPDValidationInputVO mpdValidationInput = new MPDValidationInputVO();
        List<MPDValidationOutputVO> mpdValidationOutput = mpdValidationService.mpdValidate(mpdValidationInput);
        return mpdValidationOutput;
    }