Ankita Sharma Ankita Sharma - 2 years ago 177
AngularJS Question

Parameters are not getting passed from angular to Web API in the POST call

Below is my angular and Web APi Code.In the post method null is getting passed instead of India
Angular Code:

HWApp.controller('TestCtrl', function ($scope, $http) {
$scope.SendData = function (Data)
$scope.whoo = Data;

url: "api/call/firstCall",
dataType: 'json',
method: 'POST',
data: "India",
headers: {
"Content-Type": "application/json"
}).success(function (response) {
$scope.whoo = response;
.error(function (error) {

Web Api Controller

public class CallController : ApiController
public string firstCallGet( )
return "Get";

public string firstCall([FromBody] string a)
return a;

Answer Source

Your data parameter should be JSON object.

data :{country : "India" }

Define a model class to automatically deserialze into.

Public class CountryViewModel{ Public string Country{get; set;} }

Web Api controller should be as follows

public class CallController : ApiController { [HttpPost] public string FirstCall( CountryViewModel in) { return in.Country; } }

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download