0

I am relatively new to WebAPI/AngularJS and am having a problem passing an object using $http.get() to a WebAPI controller from an AngularJS controller.

AngularJS Controller Code:

$http.get('api/Employee/GetVacancyStatus?emp='+thisData[0])
.then(function (response, success) {
    if (response.data == false){
        alert("cube/office occupied");
    }
    else // continue with employee move
    {
        do some stuff...
    }
}, function (response, error) {
   alert(response);
});

Web Api Controller Code:

 [System.Web.Http.HttpGet, System.Web.Http.ActionName("GetVacancyStatus")]
    public bool GetVacancyStatus([FromUri] employee emp)
    {
        //return repository.GetVacStatus(moveQuery);
        return false;
    }

When I debug the code, the object appears to be populated in the AngularJS Controller (at least from looking at my Chrome developer tools):

enter image description here

On the other hand, when execution hits the WebAPI controller, it seems that the object is uninitialized and I can't determine what I am doing wrong.

enter image description here

I do have an $http.put() that seems to be working passing an object like this:

 $http.put('api/Employee/PutEmployee', emp)

Thanks in advance for any assistance.

2
  • 1
    You cannot pass object through URL. You can pass only string. Commented Mar 2, 2017 at 20:57
  • 1
    You should probably just be passing the id of the employee. Are the other properties necessary for knowing the vacancy status? In addition they are values coming from client-side, so you can't trust them to be correct anyway. Commented Mar 2, 2017 at 21:07

1 Answer 1

2

Each property of the employee object you want to pass in has to be a separate query parameter. You have to do the following in your javascript:

$http.get('api/Employee/GetVacancyStatus?Id='+thisData[0].Id + '&fName=' + thisData[0].fName)

You don't have to change your controller with this method.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.