I am using AngularJS and I am trying to send in a json from my service to the webAPI controller. When I send via I recieve null in the parameter in webApi function.
My function service is:
angular.module('productsApp')
.service('ProductDetailService', ['$http', function ($http) {
var urlBase = "/api/productdetail";
this.Salvar = function (product) {
var mydata = JSON.stringify(product);
debugger;
return $http({
method: 'POST',
url: urlBase + "/salvar/" + mydata,
data: mydata,
headers: { 'Content-Type': 'application/json' }
});
};
}]);
my code in the webAPI is:
public class ProductDetailController : BaseController
{
[Route("api/productdetail/salvar/{item}")]
[HttpPost]
public bool Salvar(string item)
{
return true;
}
}
my app.js is:
var app = angular.module('productsApp', ['ngRoute']);
app.config(['$routeProvider', function ($routeProvider)
{
$routeProvider.when('/', {
controller: 'ProductController',
templateUrl: '/Scripts/App/Html/ProductList.html'
}).
when('/testprice', {
controller: 'ProductController',
templateUrl: '/Scripts/App/Html/ProductDetail.html'
}).
when('/editar/1', {
controller: 'ProductController',
templateUrl: '/Scripts/App/Html/ProductDetail.html'
}).
when('/api/productdetail/salvar/*', {
controller: 'ProductDetailController',
templateUrl: '/Scripts/App/Html/ProductDetail.html'
})
.otherwise({ redirectTo: '/' });
}]);
The problem in the service is when I am adding to type data: something in I am recieving in the webService null and I have to add my data in the complet uri, something like:
http//.../api/productdetail/salvar/{mydata}
using it, it is working.
what is wrong?