国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

解決angular的post請求后SpringMVC后臺接收不到參數(shù)值問題的方法

2019-11-20 11:02:45
字體:
供稿:網(wǎng)友

這是我后臺SpringMVC控制器接收isform參數(shù)的方法,只是簡單的打出它的值:

@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;   }

前臺頁面發(fā)送一個post提交表單的請求


發(fā)現(xiàn)后臺沒有取到值

后邊我想到的第一種方案是在控制器方法參數(shù)里加requestbody來接收json參數(shù),改成如下:

@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的值結(jié)果還是為null,
接著我又對比了下以前的項目中,接收post請求的參數(shù),發(fā)現(xiàn)一個有趣的現(xiàn)象,

下邊是Angular的默認(rèn)請求頭:

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

Content-Type: application/json

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

Content-Type: application/json

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

而jquery的post請求的"Content-Type"默認(rèn)為" application/x-www-form-urlencoded",于是我更改了angular的默認(rèn)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的值,

又查了半天,在一個老外的博客上發(fā)現(xiàn)了原因:
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
自己翻譯了:

默認(rèn)情況下,jQuery傳輸數(shù)據(jù)使用Content-Type: x-www-form-urlencodedand和類似于"foo=bar&baz=moe"的序列,然而AngularJS,傳輸數(shù)據(jù)使用Content-Type: application/json和{ "foo": "bar", "baz": "moe" }這樣的json序列。

所以下把Content-Type設(shè)置成x-www-form-urlencodedand之后,還需要轉(zhuǎn)換序列的格式,

下邊是我經(jīng)過老外實踐而自己測試過的最終方案:

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模塊中添加以上代碼,我們來看下效果:


發(fā)現(xiàn)與jquery的post請求風(fēng)格一致了,有木有!!!

看下后臺的參數(shù)接收情況,

 

isform已經(jīng)可以正常接收到參數(shù)了,大功告成!

以上就是angular的post請求后臺接收參數(shù)為null的解決方案,希望對大家的學(xué)習(xí)有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 班玛县| 台南市| 龙南县| 兴海县| 长阳| 麻栗坡县| 萍乡市| 德州市| 九龙坡区| 西乌珠穆沁旗| 永靖县| 南陵县| 琼结县| 肃北| 沁水县| 龙江县| 云霄县| 闸北区| 镇江市| 高陵县| 潮安县| 黔西县| 双桥区| 祁门县| 唐海县| 昌黎县| 伊川县| 古浪县| 曲阳县| 上蔡县| 平塘县| 登封市| 阆中市| 西贡区| 额尔古纳市| 九江市| 郧西县| 泸州市| 巴彦淖尔市| 武冈市| 秦皇岛市|