1.問題緣由jquery
默認狀況下,jQuery傳輸數據使用Content-Type: x-www-form-urlencodedand和相似於"name=zhangsan&age=18"的序列,然而AngularJS,傳輸數據使用Content-Type: application/json和{ "name": "zhangsan", "age": "18" }這樣的json序列。angularjs
2.解決辦法ajax
A.服務端進行修改,在Controller接收參數的方法中,對象前加 @RequestBody (注:要用對象的方式來接參數)
B.客戶端修改json
$http.post(url,{},{params:{"name": "zhangsan", "age": "18"}}).success(function(data){
});
$http.get(url,{params:{"name": "zhangsan", "age": "18"}}).success(function(data){ });
C.修改Angular的$httpProvider的默認處理:http://victorblog.com/2012/12/20/make-angularjs-http-service-behave-like-jquery-ajax/ (適用於post,推薦)app
var starter = angular.module('module',['ionic','ui.router']).run(function(){});
starter.config(['$httpProvider', function($httpProvider) { $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'; var param = function(obj) { var query = '', 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; }; $httpProvider.defaults.transformRequest = [function(data) { return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data; }]; }]);
書上說到能夠經過 加上 $httpProvider.defaults.headers.get['DNT']='1' 能夠解決GET請求,測試報錯,待解決。ionic