【发布时间】:2016-09-27 04:36:05
【问题描述】:
下面是我的角度和 Web APi 代码。在 post 方法中,null 被传递而不是印度 角度代码:
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 控制器
public class CallController : ApiController
{
[HttpGet]
public string firstCallGet( )
{
return "Get";
}
[HttpPost]
public string firstCall([FromBody] string a)
{
return a;
}
}
【问题讨论】:
-
我也试过这个 data: { data: 'India' } 但同样的 null 被传递了。
-
到目前为止我所理解的复杂类型 [FromUri] 是必需的,但对于我们需要的简单类型 [FromBody] 我在这里使用简单的。在上面的文章中,他们使用的是复杂类型。
标签: angularjs asp.net-web-api angular-ui-router asp.net-web-api2 asp.net-web-api-routing