Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a client application and web api. i have been call web api from angularjs, but when i send data from angularjs to web api Post Method, i receive null.


i want to send an object from angularjs to web api.

What I have tried:

i have call web api from angularjs, but when i pass data it received null in post method

JavaScript
$scope.postdata = function (name, age, adress) {

var data = {

name: name,

age: age,

adress: adress

};

//Call the services

    $http.post('http://localhost/InovoTech.Services.DataBridge/api/users/postData',$scope.data,
        {
            headers: {
                'Content-Type': 'application/json'
            }
        }


    ).then(function (response) {

if (response.data)

$scope.msg = "Post Data Submitted Successfully!";

}, function (response) {

$scope.msg = "Service not Exists";

$scope.statusval = response.status;

$scope.statustext = response.statusText;

$scope.headers = response.headers();

});



C#
[HttpPost]
[Route("postData")]
[ResponseType(typeof(Data))]
public IHttpActionResult PostUser(Data per)
{
    return null;
    //return CreatedAtRoute("DefaultApi", new { id = user.UserName }, user);
}
Posted
Updated 8-Dec-16 5:30am
Comments
Saineshwar Bageri 6-Dec-16 1:26am    
The value which you are posting from angularjs are similar to model (Data).
var data = {
name: name,
age: age,
adress: adress
};
because if it is not same then model binding will not work here.

1 solution

To force Web API to read a simple type from the request body, add the [FromBody] attribute to the parameter:


C#
[HttpPost]
[Route("postData")]
[ResponseType(typeof(Data))]
public IHttpActionResult PostUser([FromBody] Data per)
{
    return null;
    //return CreatedAtRoute("DefaultApi", new { id = user.UserName }, user);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900