默认情况下,angularjs通过post和put提交的参数是以json形式提交的,
某些情况下需要application/x-www-form-urlencoded形式的数据,就需要在执行http时重写transformRequest。
比如:
- $http({
- method: 'POST',url: url,headers: {'Content-Type': 'application/x-www-form-urlencoded'},transformRequest: function(obj) {
- var str = [];
- for(var p in obj)
- str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
- return str.join("&");
- },data: {username: $scope.userName,password: $scope.password}
- }).success(function () {});