2016-09-20 117 views
0

我是Angular的新手,想知道如何发布数据。使用angularJS发布数据到API

我有角做了一个控制器:

myApp.factory('employeeServices', ['$http', function ($http) { 

    var factoryDefinitions = { 
    moveToBench: function (employeeId) { 
      var data = $.param({ 
       json: JSON.stringify({ 
        "entityId": employeeId, 
        "nextStateId": myApp.state.bench 
       }) 
      }); 
return $http.post(myApp.IndecommBaseUrl + '/Workflow',data).success(function (data) { 
       return data; 
      }); 
     } 
    } 

    return factoryDefinitions; 
    } 
]); 

//Controller 
myApp.controller('getEmployeesController', ['$scope', 'employeeServices', 'dataTable', function ($scope, employeeServices, dataTable) { 
    employeeServices.getEmployees().then(function (result) { 
    $scope.moveToBench = function (id) { 

      employeeServices.moveToBench(id); 
     } 

    }); 
}]); 

我在HTML按钮:

<button class="btn btn-success" ng-click="moveToBench(employee.employee.id)">Move To Bench</button> 

按下按钮,我可以看到,在值:

moveToBench: function (employeeId) { 
      var data = $.param({ 
       json: JSON.stringify({ 
        "entityId": employeeId, 
        "nextStateId": myApp.state.bench 
       }) 

是正确的。但是,尽管达到了API,其值为NULL ..

后端API控制器:

// POST api/values 
     [HttpPost] 
     public void Post(int entityId, int nextStateId) 
     { 
      JObject jsonObject = JObject.Parse(System.IO.File.ReadAllText("Config.Json")); 
      string jsonFile = jsonObject.GetValue("WorkfowJsonFileLocation").ToString(); 
      var nextState = _stateServices.Get(nextStateId); 
      var handler = new WorkflowHandler(nextState, jsonFile, _entityServices, 1, _stateServices, _subStateServices, _appServices); 
      handler.PerformAction(entityId); 
     } 

谁能帮我这个?

回答

0

似乎在设置params之前调用return语句。请尝试在线版本:

moveToBench: function (employeeId) { 
    return $http.post(myApp.IndecommBaseUrl + '/Workflow', $.param({ 
       json: JSON.stringify({ 
         "entityId": employeeId, 
         "nextStateId": myApp.state.bench 
         }) 
       })) 
      .success(function (data) { 
       return data; 
      }); 
     } 
+0

它给我一些语法错误......让我再尝试修复 – Phoenix

+0

噢,抱歉,括号中是不均衡的。我编辑了我的答案。 – kenda

+0

我做了更改。但仍然得到NULL值。还弹出一个新的错误:“加载资源失败:服务器响应的状态为500(内部服务器错误)”和“否”Access-Control-Allow-Origin'标头出现在请求的资源上。 :// localhost:51703'因此不被允许访问,响应的HTTP状态码为500.“ – Phoenix