0

I am new to Angular and would like to know how to post data.

I have a controller made in Angular :

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);
        }

    });
}]);

I have a button in HTML :

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

On button press, I can see that the values in :

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

are correct. But while it reaches the API, its value is NULL..

The backend API controller is :

// 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);
        }

Can anyone help me out with this?

0

1 Answer 1

0

Seems like the return statement is called before the params are set. Please try the inline version:

moveToBench: function (employeeId) {
     return $http.post(myApp.IndecommBaseUrl + '/Workflow', $.param({
                json: JSON.stringify({
                        "entityId": employeeId,
                        "nextStateId": myApp.state.bench
                      })
                }))
            .success(function (data) {
                return data;
            });
        }
Sign up to request clarification or add additional context in comments.

7 Comments

its giving me some syntax errors... lemme try fix that
yeah sorry, parantheses were not balanced. i edited my answer.
I made the changes. But still im getting NULL value. Also a new error popped up : "Failed to load resource: the server responded with a status of 500 (Internal Server Error)" and "No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost:51703' is therefore not allowed access. The response had HTTP status code 500."
CORS is enabled. so it might not how it looks like''
Hm I'm not an asp expert but are you sure that you want your parameters sent as json string when the server expects two parameters?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.