Advertisement
jean_luucas

Post Data Null - AngularJS

Mar 5th, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. app.config(function($httpProvider){
  2.  
  3.     $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
  4.  
  5.     var param = function(obj){
  6.  
  7.         var query = '', name, value, fullSubName, subName, subValue, innerObj, i;
  8.      
  9.         for(name in obj){
  10.  
  11.             value = obj[name];
  12.        
  13.             if(value instanceof Array){
  14.                 for(i=0; i<value.length; ++i){
  15.                     subValue = value[i];
  16.                     fullSubName = name + '[' + i + ']';
  17.                     innerObj = {};
  18.                     innerObj[fullSubName] = subValue;
  19.                     query += param(innerObj) + '&';
  20.                 }
  21.             }
  22.             else if(value instanceof Object){
  23.                 for(subName in value){
  24.                     subValue = value[subName];
  25.                     fullSubName = name + '[' + subName + ']';
  26.                     innerObj = {};
  27.                     innerObj[fullSubName] = subValue;
  28.                     query += param(innerObj) + '&';
  29.                 }
  30.             }
  31.             else if(value !== undefined && value !== null)
  32.                 query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
  33.             }
  34.      
  35.             return query.length ? query.substr(0, query.length - 1) : query;
  36.  
  37.         };
  38.  
  39.     $httpProvider.defaults.transformRequest = [function(data){
  40.         return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
  41.     }];
  42.  
  43. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement