0

I have a table. Every cell of the row tables are form fields. Also, I have two buttons: one button add a new row to the table and a second one whitch send all the rows.

This is for add new blank rows:

$scope.atributs =[];

$scope.addRow = function(){
     var newRow = {nomAtribut: "",tipus: "",mida: "",prioritat: "",obligatori: "",observacions: ""};
$scope.atributs.push(newRow);
                        }

View :

    <table class="table">
            <tr>
               <td>Nom atribut</td>
                <td>Tipus</td>
                <td>Mida</td>
                <td>Prioritat</td>
                <td>Obligatori</td>
                <td>Observacions</td>
              </tr>
              <tr ng-repeat="atribut in atributs">
                 <td><input type="text" ng-model="atribut.nomAtribut" /> </td>
                 <td>
                   <select ng-options="option as option.valor for option in options" ng-model="atribut.tipus"></select>
                 </td>
                 <td><input type="number" ng-model="atribut.mida" /></td>  
                 <td>
                     <select ng-options="option as option.valor for option in options" ng-model="atribut.tipus"></select>
                  </td>
                  <td><input type="checkbox" ng-model="atribut.obligatori" ng-true-value="'YES'" ng-false-value="'NO'"></td>
                  <td><input type="text" ng-model="atribut.observacions" /> </td>
                </tr>
   </table>

Angular Code for sending data to web Service :-

$scope.sendRow = function(){
 $http.post("http://10.0.203.73/WS/ws.php/tipusactius/alta", $scope.atributs).success(function(data){
                        $scope.status = data;
                    })
                }

Json Array Sending:-

 [{"nomAtribut":"j",
 "tipus":{"idvalors_combo":"3","valor":"Date"},
  "mida":11,"prioritat":"",
  "obligatori":"YES",
  "observacions":"fcefwq"}]

Is all correct and the problem come from the web service? or the angular code is wrong? It's my first try with angular. Thank you.

Prasad
  • 1,562
  • 5
  • 26
  • 40
jcobo1
  • 1,065
  • 1
  • 18
  • 34
  • Edit: the web service expects for a JSON object, don't know if this is helpful – jcobo1 Mar 03 '16 at 11:01
  • Sounds like the first thing you need to do is open the dev tools and check whether the service call is returning any error. If it's not, check the network tab and inspect the response to see if it's returning the format you expect. – David John Smith Mar 03 '16 at 11:11
  • I will also point out that the data you've posted is an array, and is technically not a json object – David John Smith Mar 03 '16 at 11:11
  • Yes, I'm sending an array but I don't know why. How can I convert it? – jcobo1 Mar 03 '16 at 11:13
  • I can't tell you what it needs to be, you'll need to know what the web service expects. Maybe something like `requestData = {someParamNameThatTheServiceExpects: $scope.atributs}` – David John Smith Mar 03 '16 at 11:40
  • @DavidJohnSmith I builed the web service with Slim framework, It's waiting for a JSON object, but don't know why I'm sending array – jcobo1 Mar 03 '16 at 11:50
  • Complete guess, try sending $scope.atributs[0] - looks like it could be an array with the object you want to send as the only element. For reference, check this out: http://stackoverflow.com/questions/19623339/is-a-list-array-valid-json – David John Smith Mar 03 '16 at 11:55
  • @DavidJohnSmith with this the web service is working fine, this is what is sending: **{"nomAtribut":"j","tipus":{"idvalors_combo":"2","valor":"Number","$$hashKey":"object:7"},"mida":11,"prioritat":"","obligatori":"YES","observacions":"wgferwgf","$$hashKey":"object:4"}** – jcobo1 Mar 03 '16 at 11:58
  • @DavidJohnSmith I don't understand how to send the complete array now – jcobo1 Mar 03 '16 at 11:58

1 Answers1

0

So it looks like your web service wants a single element from your data array at a time.

I think you should modify the sendRow function to take the row index you want to send, like so:

$scope.sendRow = function(atributRow){
 $http.post("http://10.0.203.73/WS/ws.php/tipusactius/alta", atributRow).success(function(data){
                        $scope.status = data;
                    })
                }

Then, you can send all rows in a loop by:

angular.forEach($scope.atributs, $scope.sendRow);

or a single row with a specific index:

$scope.sendRow($scope.atributs[0])
David John Smith
  • 1,824
  • 2
  • 18
  • 22
  • I will check the web service about the single element but anyway my script doesn't send a multi element json properly formed – jcobo1 Mar 04 '16 at 07:52