1

enter image description here

enter image description here

enter image description here

Angular Post method not sending data to web api Controller. $http call to web api but its null.

2
  • use ng-model insted of data-ng-model at all, let me know Commented Oct 27, 2016 at 10:41
  • Yes i think Rakeschand is right, you need to initialize, let me to upvote Commented Oct 27, 2016 at 10:43

4 Answers 4

5

Define(Initialize) $FirstName, LastName, Email, Number as null first.

$scope.FirstName = null;
$scope.LastName = null;
$scope.Email = null;
$scope.Number = null;

or You could just use $scope.user as

$scope.user = {
    id : 0,
    FirstName : null,
    LastName : null,
    Email : null,
    Password : null
};

And pass in ng-model as user.FirstName

EDIT:

Build $scope.user outside the CreateUser Function

$scope.CreatUser=function(){
   $http{(...)};
}

ENDS

Sign up to request clarification or add additional context in comments.

4 Comments

$scope.FirstName = null; $scope.LastName = null; $scope.Email = null; $scope.Number = null; $scope.User = { Id:'0', FirstName: '', LastName: '', EMail: '', Password: '', Mobile:'0' };
$scope.CreateUser = function () { debugger; $http({ method: 'POST', dataType:'json', url: 'localhost:50048/api/SingUp/Post', data: $scope.User, headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, }).then(function successCallback(response) { $scope.productsData.push(response.data); $scope.clear(); alert("Successfully !!!"); }, function errorCallback(response) { }); };
I've change my code but still its pass null , i've also remove data and now i just use ng-model.
Build $scope.user outside CreateUser function, it's re initiating inside the CreateUser method
1

Move $scope.User to the controller scope :

app.controller('myCtrl',function($scope,$http){
   $scope.User = {
    id : 0,
    FirstName : null,
    LastName : null,
    Email : null,
    Password : null
    };

    $scope.CreatUser=function(){
      $http{(...)};
    }

})

And pass in ng-model as User.FirstName.

5 Comments

depanding on this that you have send to me ,i.sstatic.net/1gjyS.png , youre not doing it right . you need to delete the $scope.user that inside the CreatUser function
if m not sending model and m sending sting value but still its sending null. can you please check header and content-type value is ok?
i will edit my answer above with the full code , give me a sec
public httpResponsMessage Post([FromBody] SignUpView DataFromUI){
i've also try with Post([FromBody] SignUpView DataFromUI) but its not working.
1
protected void Application_BeginRequest() {
  if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS"){
    Response.Flush();
  }
}

Comments

1

you are facing this issue because your header request content-type is : application/x-www-form-urlencoded which will not work correctly. Try change it to application/json

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.