AngularJS SpringMVC解决post参数获取失败

来源:互联网 发布:linux系统管理技术手册 编辑:程序博客网 时间:2024/05/17 17:58

AngularJS SpringMVC解决post参数获取失败

在angularjs中添加以下代码,后台可以项springmvc直接根据name获取值一样获取参数

代码块

/** * 解决post 后台接受不到参数问题 */processApp        .config([                '$httpProvider',                function($httpProvider) {                    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';                    var param = function(obj) {                        var query = '', name, value, fullSubName, subName, subValue, innerObj, i;                        for (name in obj) {                            value = obj[name];                            if (value instanceof Array) {                                for (i = 0; i < value.length; ++i) {                                    subValue = value[i];                                    fullSubName = name + '[' + i + ']';                                    innerObj = {};                                    innerObj[fullSubName] = subValue;                                    query += param(innerObj) + '&';                                }                            } else if (value instanceof Object) {                                for (subName in value) {                                    subValue = value[subName];                                    fullSubName = name + '[' + subName + ']';                                    innerObj = {};                                    innerObj[fullSubName] = subValue;                                    query += param(innerObj) + '&';                                }                            } else if (value !== undefined && value !== null)                                query += encodeURIComponent(name) + '='                                        + encodeURIComponent(value) + '&';                        }                        return query.length ? query.substr(0, query.length - 1)                                : query;                    };                    $httpProvider.defaults.transformRequest = [ function(data) {                        return angular.isObject(data)                                && String(data) !== '[object File]' ? param(data)                                : data;                    } ];                } ]);
0 0