Mam problem z dodaniem użytkownika do bazy. Sama operacja dodawania kończy się pomyślnie, bo użytkownik zostaje dodany, natomiast w Console w przeglądarce pojawia się błąd :
POST http://localhost:53544/api/Users/PostUser 400 (Bad Request) i linijka z angular.js: 12011 gdzie jest błąd :
xhr.send(isUndefined(post) ? null : post);
.i błąd: Failed to load resource: the server responded with a status of 400 (Bad Request).
Mój service z metodą POST (content-type ustawiony na application/x-www-form-urlencoded nie działa również):
var ApiService = function ($http) { var result; this.PostMethod = function (obj) { var request = $http({ method: 'POST', url: 'api/Users/PostUser', data: obj, headers: { 'Content-Type': 'application/json' } }) .then(function (response) { return response.statusText; }, function (response) { return response.statusText; }); return request; } }
Metoda dodania użytkownika z kontrolera:
var RegisterController = function ($scope, Api, $http) { $scope.CreateUser = function () { var request = { Login: $scope.login, Password: $scope.password, Name: $scope.name, Surname: $scope.surname, Email: $scope.email, PhoneNumber: $scope.phoneNumber, DateOfBirth: $scope.dateofBirth, AccountTypeId: 1 } Api.PostMethod(request).then(function (d) { $scope.CreateMessage = d; alert("Komunikat") }); }; } RegisterController.$inject = ['$scope', 'Api', '$http'];
i metodą z kontrolera API:
public IHttpActionResult PostUser(User user) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.Users.Add(user); db.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = user.Id }, user); }