修改angular post提交json数据的格式_ $httpProvider配置

修改angular post提交默认格式是json数据的格式,我们如果想用$_Post接收数据的话,那么就得配置$httpProvider

$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
$httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';

  1. .config(['$httpProvider',
  2.           function ($httpProvider) {
  3.               // Use x-www-form-urlencoded Content-Type
  4.               $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
  5.               $httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';


  6.               // Override $http service's default transformRequest
  7.               $httpProvider.defaults.transformRequest = [function(data)
  8.               {
  9.                   /**
  10.                    * The workhorse; converts an object to x-www-form-urlencoded serialization.
  11.                    * @param {Object} obj
  12.                    * @return {String}
  13.                    */
  14.                   var param = function(obj)
  15.                   {
  16.                       var query = '';
  17.                       var name, value, fullSubName, subName, subValue, innerObj, i;
  18.                       console.log(obj);


  19.                       for(name in obj)
  20.                       {
  21.                           value = obj[name];
  22.                           console.log(value);


  23.                           if(value instanceof Array)
  24.                           {
  25.                               console.log("Array");
  26.                               for(i=0; i<value.length; ++i)
  27.                               {
  28.                                   subValue = value[i];
  29.                                   fullSubName = name + '[' + i + ']';
  30.                                   innerObj = {};
  31.                                   innerObj[fullSubName] = subValue;
  32.                                   query += param(innerObj) + '&';
  33.                               }
  34.                           }
  35.                           else if(value instanceof Object)
  36.                           {
  37.                                console.log("object");
  38.                               for(subName in value)
  39.                               {


  40.                                   subValue = value[subName];
  41.                                   if(subValue != null){
  42.                                       // fullSubName = name + '[' + subName + ']';
  43.                                       //user.userName = hmm & user.userPassword = 111
  44.                                       fullSubName = name + '.' + subName;
  45.                                       // fullSubName =  subName;
  46.                                       innerObj = {};
  47.                                       innerObj[fullSubName] = subValue;
  48.                                       query += param(innerObj) + '&';
  49.                                   }
  50.                               }
  51.                           }
  52.                           else if(value !== undefined ) //&& value !== null
  53.                           {
  54.                               console.log("undefined");
  55.                               query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
  56.                           }
  57.                       }


  58.                       return query.length ? query.substr(0, query.length - 1) : query;
  59.                   };


  60.                   return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
  61.               }]


  62.               $httpProvider.defaults.useXDomain = true;
  63.               // delete $httpProvider.defaults.headers.common['X-Requested-With'];
  64.           }])
复制代码


参考: http://blog.sina.com.cn/s/blog_5be1dc830101k2ds.html
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值