【问题标题】:Issue sending JSON from array to web service on Angular在 Angular 上将 JSON 从数组发送到 Web 服务的问题
【发布时间】:2016-03-03 10:58:28
【问题描述】:

我有一张桌子。行表的每个单元格都是表单域。另外,我有两个按钮:一个按钮将新行添加到表中,第二个按钮发送所有行。

这是用于添加新的空白行:

$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 服务发送数据的 Angular 代码:-

$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"}]

一切都正确并且问题来自网络服务吗?还是角码错了?这是我第一次尝试使用 Angular。谢谢。

【问题讨论】:

  • 编辑:网络服务需要一个 JSON 对象,不知道这是否有帮助
  • 听起来您需要做的第一件事是打开开发工具并检查服务调用是否返回任何错误。如果不是,请检查网络选项卡并检查响应以查看它是否返回了您期望的格式。
  • 我还要指出,您发布的数据是一个数组,在技术上不是 json 对象
  • 是的,我正在发送一个数组,但我不知道为什么。如何转换它?
  • 我不能告诉你它需要什么,你需要知道网络服务期望什么。也许像requestData = {someParamNameThatTheServiceExpects: $scope.atributs}

标签: javascript angularjs json web-services


【解决方案1】:

所以看起来您的 Web 服务一次只需要数据数组中的一个元素。

我认为您应该修改 sendRow 函数以获取您要发送的行索引,如下所示:

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

然后,您可以通过以下方式循环发送所有行:

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

或具有特定索引的单行:

$scope.sendRow($scope.atributs[0])

【讨论】:

  • 我将检查有关单个元素的 Web 服务,但无论如何我的脚本没有发送格式正确的多元素 json
猜你喜欢
  • 2014-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-31
相关资源
最近更新 更多