Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting a JS object into $http service [duplicate]

I am trying to insert an object formatted like this:

$scope.newObj = {1: "N/A", 2: "KO", 3: "OK", 4: "OK", 5: "OK", 15: "N/A", 19: "OK"} 

I tried using the following for loop:

var objt = $scope.newObject;
      console.log($scope.newObject[0]) // undefined
for(i=0;i<$scope.newObj.length;i++)
{
   $http.post("insert?etat="+$scope.newObject[0]+"&id="+Object.keys(objt)) 
}

But it doesn't seem to work. I am getting undefined everywhere. Does anyone have an idea on how to retrieve the data from that object row by row then insert the values to the service?

like image 844
Kamel Mili Avatar asked May 30 '16 16:05

Kamel Mili


1 Answers

newObj is an object rather than a array. You have tried to use it like a array. So just a iteration code needs to modify:

Use the following code:

var objt = $scope.newObject;
for(var key in objt)
{
   $http.post("insert?etat="+objt[key]+"&id="+key) 
}
like image 89
abhinsit Avatar answered Sep 30 '22 15:09

abhinsit