angularjs 的post 请求该如何调用?
简单示例:
// post 携带参数访问 $http({ method:'post', url:postUrl, data:{name:"aaa",id:1,age:20} }).success(function(req){ console.log(req); });
上面这种方法还是有些问题,携带的参数并不能发送给后台。结果为null,这是因为要转换为 form data:
可以参考:
https://blog.csdn.net/fengzijinliang/article/details/51897991
解决示例:
$http({ method:'post', url:postUrl, 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); });