$http post传值的问题
1 var app = angular.module("myApp", [], function ($httpProvider) {
2 $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'; 3 /** 4 * The workhorse; converts an object to x-www-form-urlencoded serialization. 5 * @param {Object} obj 6 * @return {String} 7 */ 8 var param = function (obj) { 9 var query = '', name, value, fullSubName, subName, subValue, innerObj, i; 10 11 for (name in obj) { 12 value = obj[name]; 13 14 if (value instanceof Array) { 15 for (i = 0; i < value.length; ++i) { 16 subValue = value[i]; 17 fullSubName = name + '[' + i + ']'; 18 innerObj = {}; 19 innerObj[fullSubName] = subValue; 20 query += param(innerObj) + '&'; 21 } 22 } 23 else if (value instanceof Object) { 24 for (subName in value) { 25 subValue = value[subName]; 26 fullSubName = name + '[' + subName + ']'; 27 innerObj = {}; 28 innerObj[fullSubName] = subValue; 29 query += param(innerObj) + '&'; 30 } 31 } 32 else if (value !== undefined && value !== null) 33 query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&'; 34 } 35 36 return query.length ? query.substr(0, query.length - 1) : query; 37 }; 38 39 // Override $http service's default transformRequest 40 $httpProvider.defaults.transformRequest = [function (data) { 41 return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data; 42 } ]; 43 44 });
1 app.controller("addController", ["$scope", "$http", function ($scope, $http) { 2 3 $scope.save = function () { 4 if ($scope.myForm.$valid) { 5 var data = { "name": $scope.txtname, "address": $scope.txtaddress, "phone": $scope.txtphone, "zipcode": $scope.txtzipcode, "isdefault": $scope.cbxisdefault }; 6 $http.post("AddUserAddress", data).success(function (result) { 7 alert(result); 8 }); 9 } 10 } 11 } ]);
只有配置了$http的默认文档类型才能再后台用Request来获取提交的参数
Content-Type