PhoneGap中文网

 找回密码
 立即注册
查看: 18821|回复: 0
打印 上一主题 下一主题

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

[复制链接]

493

主题

2035

帖子

6894

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
6894
跳转到指定楼层
楼主
发表于 2015-8-30 22:21:07 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
修改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 下一条

ionic4视频教程

Archiver|手机版|小黑屋| PhoneGap中文网 ( 京ICP备13027796号-1 )  

GMT+8, 2024-4-26 20:28 , Processed in 0.040060 second(s), 29 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表