【问题标题】:Inserting data into database by WEB API in angular JS在angular JS中通过WEB API将数据插入数据库
【发布时间】:2015-08-21 14:58:10
【问题描述】:

我是 Angular JS 和 web api 世界的新手。最近面临通过 web api 和 angular 将数据发送到数据库的困难。我能够从 webapi 加载 json 数据。任何帮助将不胜感激。 我的html:

<div class="container-fluid" ng-controller="SubmitDataCtrl">
     <div class="container text-center" style="margin-bottom:5px">
         <div id="divTables">
             <table id="tbl_SMDetails" class="display responsive nowrap aleft" cellspacing="0" width="100%">
                 <thead>
                     <tr>
                         <th class="all">Name</th>
                         <th class="all">CTSID</th>
                         <th class="all">FDID</th>
                         <th class="all">Name of the Project</th>
                         <th class="all">Effort Hour</th>
                         <th class="all">Month</th>

                     </tr>
                 </thead>

                 <tbody>
                     <tr ng-repeat="showData in loadData">
                         <td>{{showData.EmpName}}</td>
                         <td>{{showData.EmpCognizantID}}</td>
                         <td>{{showData.FDID}}</td>
                         <td><input id="Text1" type="text" />{{showData.projectName}}</td>
                         <td><input id="Text1" type="text" />{{showData.effortHour}}</td>
                         <!--<td>{{date | date:'MM-dd-yyyy}}</td>-->
                         <td>{{showData.date}}</td>
                     </tr>
                 </tbody>
             </table>
         </div>

     </div>
   <div>
       <a ng-href='#SubmitData' class="btn  btn-default" ng-click='go()'>Submit</a><span></span>
       <a ng-href='#SubmitData' class="btn  btn-default" ng-click=''>Reset</a>

   </div> 
     </div>

我的服务页面如下所示:

var request = $http({
                        method: method_args,
                        url: myConfig.ServiceURL + url,
                        data: JSON.stringify(input_data),
                        Accept: 'application/json',
                        headers: {'Content-Type':'application/json'},
                        xhrFields: {
                            withCredentials: true
                        }
                    });
                }

我的控制器是

App.controller("SubmitDataCtrl", function($scope, ajaxService) {
//login user    
ajaxService.MakeServiceCall("employee/GetTeamMember?         superVisiorCogniID=256826", "GET", "")
.then(function effortData(data)
{
  // alert(data);
  $scope.date = new Date();
  $scope.loadData = data;
  showData = $scope.loadData;
  for (var i = 0; i < showData.length; i++)
  {
      //alert(showData[i].EmpName + showData[i].EmpCognizantID + showData[i].FDID);
  }});

$scope.go = function()
{

   //alert('clicked');
   ajaxService.MakeServiceCall("effort/SaveTeamEfforts?hourLoadDate=08/20/2015&hourLoadByCogniID=256826&forMonth=July", "POST", Employee).then(function save() {
        var Employee =
            {
                "EmpCogniID": showData[i].EmpCognizantID,
                "FDTimeCardHour": showData[i].effortHour,
                "HourLoadDate": 08/11/2015,
                "HourLoad`enter code here`By": "256826",`enter code here`
                "ForMonth": "july"
            }
   }).success(function (Employee)
   {
       $scope.showData[i] = Employee;
   });
   alert(Employee);    }});

【问题讨论】:

  • 您遇到错误了吗?它根本没有访问网络服务?
  • 它没有访问 Web 服务。我在 go 函数中的服务调用处添加了一个调试点,但它在取消注释时显示弹出消息后退出。该 api 是正确的,因为它在浏览器中打开时显示了一个 xml 页面。

标签: json angularjs api post web


【解决方案1】:

如果不知道 API POST 调用应该做什么,就很难回答。

您能否通过在浏览器中使用 REST 测试器执行来确认您为努力/保存团队努力所做的 POST 是否有效?

如果这有效,您应该在 API 调用中添加一个 .error 函数,并查看 dan 上面所建议的返回什么错误

L

【讨论】:

  • post 方法将在数据库中插入值。 var employee 中双引号内的名称是表中的列名。前 2 个值应来自页面,后 3 个值是硬编码的。当我在浏览器中提供完整的 url 时,Api 可以从提琴手完成时手动插入值,它会打开一个 xml 页面。不知何故,我的功能无法正常工作,因此它没有从控制器正确调用 web api。
猜你喜欢
  • 2021-11-08
  • 2017-06-21
  • 2014-01-13
  • 1970-01-01
  • 1970-01-01
  • 2016-07-21
  • 2015-12-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多