简体   繁体   English

从数组中删除值并将其添加到POST方法请求中

[英]Removing values from array and adding them to POST method request

I have a cart in my AngularJS app and I need to identify the items through their id through a POST method. 我的AngularJS应用中有一个购物车,我需要通过POST方法通过其ID识别商品。

They are stored in an array called patent_ids and are appended to the request like so: 它们存储在一个名为patent_ids的数组中,并附加到请求中,如下所示:

 var cartArr = [];
 var cartItems = ngCart.getItems()

 cartItems.forEach(function(currentValue, index, array){
     cartArr.push(currentValue._id)
  })

  var obj = {
      patent_id: cartArr
  };

  fetchBasketPatents(obj);

function fetchBasketPatents(obj) {
    //load of code to call service and pass the ids
}

var REST_SERVICE_URI = 'http://localhost:8080/p3sweb/rest-basket/';

factory.fetchBasketPatents = function(ids) {

    var deferred = $q.defer();

    $http.post(REST_SERVICE_URI, ids) //REQUEST TO BACK END WITH IDS
    .then(
        function(response){
            deferred.resolve(response.data)
        },
        function(errResponse){
            deferred.reject(errResponse)
        }
    )

    return deferred.promise;

}

It works but it would be easier if rather than sending the ids in a named array, they could be sent as an anonymous array, or even better each item extracted an inserted into an anonymous object. 它可以工作,但是如果不将ID以命名数组的形式发送,而是将其作为匿名数组发送,甚至更好的是,将提取到一个插入对象的每个项提取到一个匿名对象中,则会更容易。

Question

How do I send the ids in the POST request in either an anonymous array or object? 如何在匿名数组或对象的POST请求中发送ID? Apologies if I am missing something obvious. 抱歉,如果我错过了明显的内容。

How do I send the ids in the POST request in either an anonymous array or object? 如何在匿名数组或对象的POST请求中发送ID?

From Document: 来自文件:

   $http.post('/someUrl', data, config).then(successCallback, errorCallback);

Where data{string|Object} 数据{string|Object}

So the answer is: you cannot define anonymous array but object only (like you did) 答案是:您不能定义匿名数组,而只能定义对象(就像您一样)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM