angular的post請求,springmvc後臺接收不到參數的解決方案

angular的post請求,後臺接收參數爲null的解決方案。

1、確定angularjs,如何使用,纔是post請求。angularjs實際開發過程,發現,想使用post請求,不僅僅需要設置,method:'POST',還需要

傳參的時候使用data (注:如果使用params傳參,angularjs默認使用 get請求),如下:

$http({
    method:'POST',
    url:baseUrl+url,
    data : data
}).then(function(result) {      
    alert(result.data);                 
});
2、angularjs的post請求的"Content-Type"默認爲" application/josn",而 jquery的post請求的"Content-Type"默認爲" application/x-www-form-urlencoded"

故,要想springmvc後臺接收到參數。需要修改angularjs的post請求的Content-Type爲x-www-form-urlencodedand。當然,此時後臺仍然接受不到數據。

3、默認情況下,jQuery傳輸數據使用Content-Type: x-www-form-urlencodedand和類似於"foo=bar&baz=moe"的序列,

然而AngularJS,傳輸數據使用Content-Type: application/json和{ "foo": "bar", "baz": "moe" }這樣的json序列。所以把Content-Type設置成x-www-form-urlencodedand

之後,還需要轉換序列的格式。

.config(function($httpProvider) {
        $httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
        $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

        // Override $http service's default transformRequest
        $httpProvider.defaults.transformRequest = [function(data) {
            /**
             * The workhorse; converts an object to x-www-form-urlencoded serialization.
             * @param {Object} obj
             * @return {String}
             */
            var param = function(obj) {
                var query = '';
                var 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;
            };

            return angular.isObject(data) && String(data) !== '[object File]'
                ? param(data)
                : data;
        }];
    })

完成以上修改,springmvc後臺已經可以正常接收到參數了,大功告成!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章