带有多个ID的多个ID(url)的Angular js $ http.put调用无法正常工作

时间:2017-03-21 11:38:34

标签: angularjs api promise

我有一个api 'rest/latest/testruns/16543558' id是test run ids。我想一次更新具有不同ID的呼叫。我试过$promise.all()。如果我提供个人ID,但如果我给出多个id给出错误

,它的工作正常

使用单个ID工作FINE:

var ids = ['16611544']; //'16611345','16611347'

$scope.updateTestRun = function(data) {
    data.showedit = true;
    $scope.label = 'Save';

    var updateform = {};
    var updateArr = [];
    for (var i = 0; i < data.length; i++) {
      var testrunid = data[i].data.data.id;
      var updatedata = data[i].data.data.fields;
      updateform['fields'] = data[i].data.data.fields;
      updateform.fields['duration'] = 1000;
      delete updateform.fields['executionDate'];

      for (var j = 0; j < data[i].data.data.fields.testRunSteps.length; j++)   {  
        data[i].data.data.fields.testRunSteps[j].status = data[i].data.data.fields.testRunStatus;
      }


      updateArr.push($http.put("rest/latest/testruns/" + testrunid, updateform))
      j = 0;
      $scope.loader = true;
   }
}

1 个答案:

答案 0 :(得分:0)

你可以使用$q.all一次发送多个请求

var fullArray = [];

for (var i = 0; i < data.length; i++) {
    var testrunid = data[i].data.data.id;
    var updatedata = data[i].data.data.fields;
    updateform['fields'] = data[i].data.data.fields;
    updateform.fields['duration'] = 1000;
    delete updateform.fields['executionDate'];
    for (var j = 0; j < data[i].data.data.fields.testRunSteps.length; j++) {
        data[i].data.data.fields.testRunSteps[j].status = data[i].data.data.fields.testRunStatus;
    }
    j = 0;
    $scope.loader = true;
    // push all request to array 

    fullArray.push($http.put("rest/latest/testruns/" + testrunid, updateform))
}
$q.all(fullArray).then(function(values) {
    //resoponse
});