我是angularjs和webapi的初学者。我创建了下面的angularjs和webapi。我面临的问题是api对象只包含null而没有数据。谁能发现我做错了什么。
Angular Code:
$scope.GetReport = function () {
var ReportModel = {};
debugger;
var result = {};
if (angular.isDefined($scope.Report.FromDate))
ReportModel.FromDate = new Date($scope.Report.FromDate);
else
ReportModel.FromDate = null;
if (angular.isDefined($scope.Report.ToDate))
ReportModel.Todate = new Date($scope.Report.ToDate);
else
ReportModel.Todate = null;
ReportModel.UserID = '';
ReportModel.UserWWID = '1234';
ReportModel.UserRole = '';
ReportModel.ProjectType = '';
ReportModel.ProjStatus = 'In Progress';
ReportModel.CreatedBy = '11744439';
ReportModel.LGroup = '';
ReportModel.LUnit = '';
ReportModel.LTeam = '';
var config = {
headers: { 'Content-Type': 'application/json;charset=utf-8' },
datatype: JSON,
data: JSON.stringify(ReportModel)
};
result = $http.get('api/Project/GetReport', { params: ReportModel })
.then(function (response) {
result = response.data;
})
}, function (response) {
alert('Failed ' + JSON.stringify(response.statusText));
};
}
Web API:
public IHttpActionResult GetReport(ViewReportModel objViewRepotModel)
{
try
{
//Code here
}
catch (Exception ex)
{
return NotFound();
}
}
答案 0 :(得分:0)
$http
将json作为默认类型,因此您可以对config进行评论,只需发送ReportModel
作为有效负载。
//var config = {
// headers: { 'Content-Type': 'application/json;charset=utf-8' },
// datatype: JSON,
// data: JSON.stringify(ReportModel)
//};
var payload = {reportModel : ReportModel};
result = $http.post('api/Project/GetReport', payload)
.then(function (response) {
result = response.data;
})
}, function (response) {
alert('Failed ' + JSON.stringify(response.statusText));
};
此外,为了更好的安全原因,我建议你在这里使用$http.Post
。