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

這是我後臺SpringMVC控制器接收isform參數的方法,只是簡單的打出它的值:
@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> save(
			@RequestParam(value = "isform", required = false) String isform) {
		System.out.println("isform value: " + isform);
		return null;

	}
前臺頁面發送一個post提交表單的請求

發現後臺沒有取到值 java

後邊我想到的第一種方案是在控制器方法參數里加requestbody來接收json參數,改爲以下: jquery

@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> save(
			@RequestParam(value = "isform", required = false) @RequestBody String isform) {
		System.out.println("isform value: " + isform);
		return null;

	}
可是isform的值結果仍是爲null,

接着我又對比了下之前的項目中,接收post請求的參數,發現一個有趣的現象, angularjs

下邊是Angular的默認請求頭, ajax

$httpProvider.defaults.headers.post: (header defaults for POST requests) json

Content-Type: application/json app

$httpProvider.defaults.headers.put(header defaults for PUT requests) ide

Content-Type: application/json post

其中Angular的post和put都是application/json, 測試

而jquery的post請求的"Content-Type"默認爲" application/x-www-form-urlencoded",因而我更改了angular的默認Content-Type,
app.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';

});
接下來的請求body變成了這樣,可是後邊仍是沒有取到isform的值,

又查了半天,在一個老外的博客上發現了緣由,連接以下: ui

http://victorblog.com/2012/12/20/make-angularjs-http-service-behave-like-jquery-ajax/

By default, jQuery transmits data using Content-Type: x-www-form-urlencoded and the familiar foo=bar&baz=moe serialization. AngularJS, however, transmits data using Content-Type: application/json and { "foo": "bar", "baz": "moe" } JSON serialization

本身翻譯了:

默認狀況下,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以後,還須要轉換序列的格式,

下邊是我通過老外實踐而本身測試過的最終方案:

app.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;
	}];
});
在angular模塊中添加以上代碼,咱們來看下效果:

發現與jquery的post請求風格一致了,有木有!!!

看下後臺的參數接收狀況,

isform已經能夠正常接收到參數了,大功告成!

相關文章
相關標籤/搜索