简体   繁体   中英

Parameters are not getting passed from angular to Web API in the POST call

Below is my angular and Web APi Code.In the post method null is getting passed instead of India Angular Code:

HWApp.controller('TestCtrl', function ($scope, $http) {
     $scope.SendData = function (Data)
      {
        $scope.whoo = Data;
        console.log(Data);

        $http({
         url: "api/call/firstCall",
         dataType: 'json',
         method: 'POST',
         data: "India",
          headers: {
          "Content-Type": "application/json"
          }
           }).success(function (response) {
            $scope.whoo = response;
            })
            .error(function (error) {
           alert(error);
            });
        }
      }
    );

Web Api Controller

  public class CallController : ApiController
    {
        [HttpGet]
        public string firstCallGet( )
        {
            return "Get";
        }


        [HttpPost]
        public string firstCall([FromBody] string a)
        {
            return a;
        }
    }

Your data parameter should be JSON object.

data :{country : "India" }

Define a model class to automatically deserialze into.

Public class CountryViewModel{ Public string Country{get; set;} }

Web Api controller should be as follows

public class CallController : ApiController { [HttpPost] public string FirstCall( CountryViewModel in) { return in.Country; } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM