2016-09-20 152 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