c# - Asp.Net Web API is not receiving Post data from angular -


please me out.

i want pass string post method of webapi controller using $http method.but getting null string @ controller side.

here client side code.

$http({         url: 'http://localhost/mywebapi/api/home',         method: 'post',         headers: { 'content-type': 'application/json' },         data: json.stringify({name: $scope.newproduct})      }).success(function (data, status, headers, config) {         $scope.products = data;         $scope.newproduct = "";         console.log(data);     })      .error(function (data, status, headers, config) {                   console.log(data);       }); 

and here webapi controller

 [httppost]         public ihttpactionresult post([frombody]string newproduct)         {             var db = new mvcentities();             db.products.add(new product() { productname = newproduct });             db.savechanges();             var products = db.products.tolist();             return ok();         } 

thanks

your json not match method signature. there no need json, can pass in normal string

data: $scope.newproduct 

or use custom object, matches json , use parameter method, like

[datacontract] public class newproductdata{     [datamember(name="name")]    public string name {get;set;} } 

new method signature

public ihttpactionresult post([frombody]newproductdata newproduct) 

Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -