$ http获取空体

时间:2017-02-09 22:15:11

标签: c# angularjs get

我在angularjs中有一个GET方法,它从C#中的API控制器接收数据。控制器正在返回数据,$ http get方法正在接收响应,但响应内容主体为空。

我的HttpGet API控制器功能:

[HttpGet]
[Route("api/LandingPage/GetByDate")]
public HttpResponseMessage GetByDate(DateTime startDate, DateTime endDate)
{
    try
    {
        var startDateParam = new SqlParameter("@startDate", startDate);
        var endDateParam = new SqlParameter("@endDate", endDate);

        var confirmData = m_databaseObject.Database.SqlQuery<PageModel>("[dbo].[GetPageData] @startDate,@endDate", startDateParam, endDateParam).ToList();

        return Request.CreateResponse(HttpStatusCode.OK, confirmData);
    }
    catch (Exception e)
    {
        return Request.CreateErrorResponse(HttpStatusCode.NotFound, $"Error occured {e.Message} {startDate:yyyy-MM-dd} {endDate:yyyy-MM-dd}");
    }
}

我想要返回的对象:

public class PageModel
{
    string Name { get; set; }
    int? FirstTotal { get; set; }
    int? FisrtInitial { get; set; }
    int? FisrtApproval { get; set; }
    int? SecondTotal { get; set; }
    int? SecondInitial { get; set; }
    int? SecondApproval { get; set; }
    int? Validated { get; set; }
    int? NotSettled { get; set; }
}

GET方法:

this.getLandingPage = function ($scope) {
    $scope.error = "";
    $scope.message = "";
    return $http({
        method: "GET",
        url: apiName + "LandingPage/GetByDate?startDate=" + $scope.StartDate + "&endDate=" + $scope.EndDate,
        headers: { 'Content-Type': 'application/json' }
    }).success(function (data) {
        if (data.length === 0)
            $scope.message = "No Data found";
        else {
            $scope.LandingPageData = data;
        }
    }).error(function (error) {
        $scope.LandingPageData = null;
        if (error)
            $scope.error = error.Message;
        else
            $scope.error = "Data services may not be running, Please check!";
    })
}

我的控制器中的confirmData变量包含函数返回时的数据。我的angularjs GET方法返回成功,data.length等于1,但data中没有包含值。

有人可以解释为什么我的数据没有正确传输吗?

4 个答案:

答案 0 :(得分:1)

事实证明我的PageModel类变量需要声明为public才能从控制器传递数据。

PageModel类更改为此工作:

public class PageModel
{
    public string Name { get; set; }
    public int? FirstTotal { get; set; }
    public int? FisrtInitial { get; set; }
    public int? FisrtApproval { get; set; }
    public int? SecondTotal { get; set; }
    public int? SecondInitial { get; set; }
    public int? SecondApproval { get; set; }
    public int? Validated { get; set; }
    public int? NotSettled { get; set; }
}

答案 1 :(得分:0)

尝试返回JSON而不是HttpResponseMessage

[HttpGet]
[Route("api/LandingPage/GetByDate")]
public JsonResult GetByDate(DateTime startDate, DateTime endDate)
{
    try
    {
        var startDateParam = new SqlParameter("@startDate", startDate);
        var endDateParam = new SqlParameter("@endDate", endDate);

        var confirmData = m_databaseObject.Database.SqlQuery<PageModel>("[dbo].[GetPageData] @startDate,@endDate", startDateParam, endDateParam).ToList();

        return new JsonResult() { Data = confirmData , JsonRequestBehavior = JsonRequestBehavior.AllowGet };
    }
    catch (Exception e)
    {
        throw;
    }
}

在客户端,您的数据应放在响应data属性中。在你的情况下应该是:

data.data

但请尝试使用then函数intead success函数:

.then(function (data) {
        var result = data.data;
    });

即使您可以避免JsonResult并使用string。将模型序列化为JSON字符串并返回字符串。

答案 2 :(得分:0)

通常,数据嵌套在data属性下。

.success(function (resp) {
    if (resp.data.length === 0)
        $scope.message = "No Data found";
    else {
        $scope.LandingPageData = resp.data;
    }
})

答案 3 :(得分:0)

您使用的是哪种角度?从1.6 .success不推荐使用:

并且,您设置Content-type标头的具体原因是什么?您可以使用更简单的方法:

return $http.get(url: apiName + "LandingPage/GetByDate?startDate=" + $scope.StartDate + "&endDate=" + $scope.EndDate, ).then(successCallback, errorCallback);

然后单独定义回调函数。