【问题标题】:formatting post data before post in angularjs在angularjs中发布之前格式化发布数据
【发布时间】:2014-08-27 08:15:15
【问题描述】:

我如何将我的cartPost 发布数据发送为:

products=
[
   {
    "code": "mig-09",
    "title": "supermig",
    "quantity": "1"
   }
]

这是我的http.post

$http.post("www.remoteurl.com", {
    products: cartPost
}).success(function(data){
  $scope.cart = data;
})

我需要这样做,因为在服务器端。帖子数据将填充到$products = json_decode($_POST['products']) 中,否则将不起作用。我该怎么做?谁能帮我解决这个问题?

【问题讨论】:

  • cartPost 是一个数组吗?
  • cartPost 包含[ { "code": "mig-09", "title": "supermig", "quantity": "1" } ]
  • 只发送有效吗? $http.post("www.remoteurl.com", cartPost)
  • 不行,post 数据必须是这样的products=[{"code":"mig-09"}] 才行
  • 这不是有效的 JSON

标签: angularjs http-post


【解决方案1】:

我找到了解决问题的方法。

$http({
    method: 'POST',
    url: 'www.remoteurl.com',
    data:'products=' + cartPost, // the solution
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(response) {
    $scope.result = response;
});

【讨论】:

    猜你喜欢
    • 2013-09-28
    • 1970-01-01
    • 2013-12-03
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 2010-11-18
    相关资源
    最近更新 更多