Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs api未接收到$http get params_Angularjs_Asp.net Web Api - Fatal编程技术网

Angularjs api未接收到$http get params

Angularjs api未接收到$http get params,angularjs,asp.net-web-api,Angularjs,Asp.net Web Api,我是angularjs和webapi的初学者。我已经创建了以下angularjs和webapi。我面临的问题是,api对象只包含null,没有数据。谁能看出我做错了什么 角度代码: $scope.GetReport = function () { var ReportModel = {}; debugger; var result = {}; if (angular.isDefined($scope.Report.FromDate)) Repo

我是angularjs和webapi的初学者。我已经创建了以下angularjs和webapi。我面临的问题是,api对象只包含null,没有数据。谁能看出我做错了什么

角度代码:

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

$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

我已经没有使用config对象,如果我通过了您显示的方式,我甚至没有看到QueryString中的参数您没有使用web api的基本路径,它应该类似于
http://localhostxxx/api/Project/GetReport
另外,请参见发送有效负载方面的更改。为什么不使用
配置对象
和post,如
$http.post('api/Project/GetReport',config)
?在编写问题时,尽可能少使用仍会产生相同问题的代码。消除与问题无关的任何问题。