1

下面通過我的角度和網絡API Code.In POST方法將null獲得通過,而不是印度 角代碼:參數沒有得到從角度對Web API中的POST調用

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); 
      }); 
     } 
     } 
    ); 

網絡API控制器

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


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

看到這個http://stackoverflow.com/questions/19254029/angularjs-http-post-does-not-send-數據 – mindparse

+0

我也試過這個數據:{data:'India'}但是相同的null正在通過。 –

+0

看看是否有幫助:http://stackoverflow.com/questions/30957248/how-to-send-post-in-angularjs-with-multiple-params/30957308#30957308 – Sajal

回答

0

您的數據參數應該是JSON對象。

data :{country : "India" }

定義模型類自動deserialze成。

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

網絡API控制器應該如下

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

+0

謝謝。有效。 –

+0

CountryViewModel中的屬性不匹配,json返回'country',模型期待'Country'。 – Sajal

+0

但是,有沒有什麼辦法可以做到而不需要反序列化? –