首頁  >  文章  >  web前端  >  解決angular的post請求後SpringMVC後台接收不到參數值問題的方法_AngularJS

解決angular的post請求後SpringMVC後台接收不到參數值問題的方法_AngularJS

WBOY
WBOY原創
2016-05-16 15:26:401608瀏覽

這是我後台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提交表單的請求


發現後台沒有取到值

後邊我想到的第一個方案是在控制器方法參數裡加requestbody來接收json參數,改成如下:

@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請求的參數,發現一個有趣的現象,

下邊是Angular的預設請求頭:

$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"預設為" 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的值,

又查了半天,在一個老外的博客上發現了原因:
By default, jQuery transmits data using Content-Type: x-www-form-urlencoded and the familiar foo=bar&baz=moe sing Content-Type: x-www-form-urlencoded and the familiar foo=bar&baz=moe serialization. AngularJS, however, transmits data datausing Cont-Typeization. "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 &#63; query.substr(0, query.length - 1) : query;
    };
 
    return angular.isObject(data) && String(data) !== '[object File]'
        &#63; param(data)
        : data;
  }];
});

在angular模組中加入以上程式碼,我們來看下效果:


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

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

 

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

以上就是angular的post請求後台接收參數為null的解決方案,希望對大家的學習有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn