0

Angular code is below. Please look if I am missing something in post request -

<script>   
var app = angular.module("myApp", [])
.controller("myControllerSubmit", function ($scope, $http) {
    $scope.submit = function () {
        var Employee = { empID: $scope.empId, Name: $scope.empName, Gender: $scope.empGender }
        $http(
        {
          method: 'POST',
          url: 'http://localhost:58365/home/saveEmpData',
          data:  Employee,
          headers: { 'Content-Type': 'application/x-www-form-urlencoded'                       }   
        }).success(function (response,status,headers,config) {
              $scope.data1 = response.data;
        })
    }
})
</script>

In webApi I am getting null Employee object

[HttpPost]
public void saveEmpData( Employee emp)
{
    if (emp.Name != null)
    {
        var objempList = new DAL();
        objempList.saveData(emp);
    }
}
ebram khalil
  • 8,071
  • 7
  • 39
  • 55
MukulSharma
  • 221
  • 2
  • 14

5 Answers5

0

Just put [FromBody] Attribute before emp parameter to get data from posted request.

public void saveEmpData([FromBody] Employee emp)
Pankaj Parkar
  • 127,691
  • 20
  • 213
  • 279
0

The content type x-www-form-urlencoded is used when sending body content as one giant string. Read Matts great answer about it here
In your case you want to send an object and then its more suitable to use the content type json.
I would change the submit as below and try again

    <script>   

          var app = angular.module("myApp", [])

        .controller("myControllerSubmit", function ($scope, $http) {

            $scope.submit = function () {
                var Employee = { empID: $scope.empId, Name: $scope.empName, Gender: $scope.empGender }
                $http(
              {
                  method: 'POST',
                  url: 'http://localhost:58365/home/saveEmpData',
                  data:  JSON.stringify(Employee),
                  headers: { 'Content-Type': 'application/json'                       }   
              }).success(function (response,status,headers,config) {
                  $scope.data1 = response.data;
              })
            }
        })

    </script>
Community
  • 1
  • 1
Marcus Höglund
  • 13,944
  • 9
  • 42
  • 63
0
try this..

          <script>   

                  var app = angular.module("myApp", [])

                .controller("myControllerSubmit", function ($scope, $http) {

                    $scope.submit = function () {
                          var Employee = { empID: $scope.empId, Name: $scope.empName, Gender: $scope.empGender }
                         $http.post( 'http://localhost:58365/home/saveEmpData', json.Stringify(Employee) ).success(function (response,status,headers,config) {
                          $scope.data1 = response.data;
                        }).error (function(){
                              alert("error")
                          });
                    }
                })

                 </script>
0

As you want model binding to occur through json, You have to change the variable to the same name as it is in Web api emp.

<script>   

              var app = angular.module("myApp", [])

            .controller("myControllerSubmit", function ($scope, $http) {

                $scope.submit = function () {
                    var Employee = { empID: $scope.empId, Name: $scope.empName, Gender: $scope.empGender }
                    $http(
                  {
                      method: 'POST',
                      url: 'http://localhost:58365/home/saveEmpData',
                      data:  {emp: Employee},
                      headers: { 'Content-Type': 'application/x-www-form-urlencoded'                       }   
                  }).success(function (response,status,headers,config) {
                      $scope.data1 = response.data;
                  })
                }
            })

             </script>

That will solve your issue hopefully

Umair Farooq
  • 1,574
  • 2
  • 11
  • 24
  • @Umair..if i use your code i am getting this error - XMLHttpRequest cannot load http://localhost:58365/home/saveEmpData. Response for preflight has invalid HTTP status code 404 – MukulSharma Aug 29 '16 at 10:09
  • I have edited the code. Just changed this line. data: Employee to data: {emp: Employee} – Umair Farooq Aug 29 '16 at 10:22
0

you made a mistake here in assigning the properties. Replace with this

var Employee = { "empID": $scope.empId, "Name": $scope.empName, "Gender": $scope.empGender }
Aravind
  • 36,165
  • 14
  • 84
  • 102