前臺頁面發送一個post提交表單的請求
發現后臺沒有取到值
后邊我想到的第一種方案是在控制器方法參數里加requestbody來接收json參數,改成如下:
但是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,
接下來的請求body變成了這樣,但是后邊還是沒有取到isform的值,
又查了半天,在一個老外的博客上發現了原因:
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之后,還需要轉換序列的格式,
下邊是我經過老外實踐而自己測試過的最終方案:
在angular模塊中添加以上代碼,我們來看下效果:
發現與jquery的post請求風格一致了,有木有!!!
看下后臺的參數接收情況,
isform已經可以正常接收到參數了,大功告成!
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com