使用的angular版本1.6.4,后台采用springMVC。
項目要實行前后台分離,前后台交互都采用json,剛開始有些抗拒。
不否認,angular確實好用,不用再花很多心思在繁瑣的dom上了。
不過小白我剛剛接觸,廢了半天勁,傳個值都沒搞定,心累。
且不論POST對應data,GET對應params的低級錯誤。
雖然后來知道是angular的$http傳值Content-Type默認是'application/json',可沒想到
headers: {'Content-Type': 'application/x-www-form-urlencoded'}必須置於最后。
//如果你想避免由於ng-repeat使得傳入的json會存在有$hashkey的問題,請使用angular.toJson對其序列化,而不是使用JSON.stringify
var data = JSON.stringify($scope.products);
$http({
method: "POST",
url: url,
data: $.param({data:data}),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).then(function (data) {
}).error(function () {
});
/*<-----------------------分割線--------------------------->*/
$http的get請求:
$http({
method: "GET",
url: "url",
params: {"param1": "param1",....},
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).then(function successCallback(response){
},function errorCallback(response){
// 請求失敗執行代碼
});
補充:
application/x-www-form-urlencoded: 窗體數據被編碼為名稱/值對。這是標准的編碼格式。
multipart/form-data: 窗體數據被編碼為一條消息,頁上的每個控件對應消息中的一個部分,這個一般文件上傳時用。
text/plain: 窗體數據以純文本形式進行編碼,其中不含任何控件或格式字符。
參考鏈接:http://stackoverflow.com/questions/11442632/how-can-i-post-data-as-form-data-instead-of-a-request-payload
http://blog.csdn.net/qq_28702545/article/details/51719199
https://www.cnblogs.com/chris-oil/p/6518633.html