问题发送JSON从阵列到角度上的Web服务
问题描述:
我有一张桌子。行表中的每个单元格都是表单字段。此外,我有两个按钮:一个按钮添加一个新的行到表和第二个发送所有行。问题发送JSON从阵列到角度上的Web服务
这是添加新的空白行:
$scope.atributs =[];
$scope.addRow = function(){
var newRow = {nomAtribut: "",tipus: "",mida: "",prioritat: "",obligatori: "",observacions: ""};
$scope.atributs.push(newRow);
}
查看:
<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>
将数据发送到Web服务的角度代码: -
$scope.sendRow = function(){
$http.post("http://10.0.203.73/WS/ws.php/tipusactius/alta", $scope.atributs).success(function(data){
$scope.status = data;
})
}
JSON数组发送: -
[{"nomAtribut":"j",
"tipus":{"idvalors_combo":"3","valor":"Date"},
"mida":11,"prioritat":"",
"obligatori":"YES",
"observacions":"fcefwq"}]
是否正确,问题来自Web服务?或角码错误?这是我第一次尝试角度。谢谢。
答
所以看起来你的web服务一次只需要你数据数组中的一个元素。
我想你应该修改sendRow功能把要发送,像这样的行索引:
angular.forEach($scope.atributs, $scope.sendRow);
:
$scope.sendRow = function(atributRow){
$http.post("http://10.0.203.73/WS/ws.php/tipusactius/alta", atributRow).success(function(data){
$scope.status = data;
})
}
然后,您可以通过发送在一个循环中的所有行
或具有特定索引的单行:
$scope.sendRow($scope.atributs[0])
+0
我将检查有关单个元素的Web服务,但无论如何,我的脚本不会发送正确形成的多元素json – proktovief
编辑:Web服务需要一个JSON对象,不知道这是否是helpf ul – proktovief
听起来你需要做的第一件事是打开开发工具并检查服务调用是否返回任何错误。如果不是,请检查网络选项卡并检查响应以查看它是否返回您期望的格式。 –
我也会指出你发布的数据是一个数组,并且在技术上不是一个json对象 –