api不接收$ http get params

时间:2017-05-22 06:48:20

标签: angularjs asp.net-web-api

我是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();
        }
    }

1 个答案:

答案 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