在角度JS中通过WEB API将数据插入数据库

时间:2015-08-21 14:58:11

标签: json angularjs api post web

我是角度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);    }});

1 个答案:

答案 0 :(得分:0)

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

您是否可以通过在浏览器中使用REST测试程序执行它来确认您正在进行的POST / saveTeamEfforts是否正常工作?

如果这样做,你应该为你的API调用添加一个.error函数,看看当上面有过消息时返回了什么错误