AngularJS $http模块POST请求,传递参数为数组或者对象时

来源:互联网 发布:g92内螺纹编程实例 编辑:程序博客网 时间:2024/06/05 22:32

代码如下:

$http({      method:'post',     url:'post.php',      data:{name:"aaa",id:1,age:20}  }).success(function(req){      console.log(req);  })

 

解决方案:

1、 

var myApp = angular.module('app',[]);myApp.config(function($httpProvider){ $httpProvider.defaults.transformRequest = function(obj){var str = [];for(var p in obj){str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));}return str.join("&");

2.  

$http({method:'post',url:'post.php',data:{name:"aaa",id:1,age:20},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("&");}}).success(function(req){console.log(req);})



阅读全文
0 0
原创粉丝点击