【问题标题】:$http GET Receiving Empty Body$http GET 接收空正文
【发布时间】:2017-02-09 22:15:10
【问题描述】:

我在 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 中不包含任何值。

谁能解释为什么我的数据没有被正确传输?

【问题讨论】:

  • 内部数据是什么?你试过data.data吗?
  • data 包含一个大小为 1 的对象数组和一个空对象
  • 你调试过你的控制器并确保confirmData确实有值吗?
  • 是的。 PageModel 对象包含控制器返回时的数据

标签: c# angularjs get


【解决方案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; }
}

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 当我尝试这个时,我得到 resp.data 未定义。
    • 您在网络控制台中看到的响应是什么?
    • 响应码为200,响应正文为[{}]
    • 你调试过你的控制器并确保confirmData确实有值吗?您可以尝试只返回一个明确定义的数组,看看是否可行。
    【解决方案3】:

    您使用的是哪个版本的 Angular?从 1.6 开始,.success 已被弃用:

    而且,您设置 Content-type 标头的具体原因有哪些?您可以使用更简单的方法:

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

    然后分别定义你的回调函数。

    【讨论】:

      【解决方案4】:

      尝试返回 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函数 inteadsuccess函数:

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

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

      【讨论】:

      • 我尝试使用 JsonResult,但我遇到了同样的问题。
      • @KevinK。尝试使用then 而不是sucess
      猜你喜欢
      • 1970-01-01
      • 2016-05-13
      • 1970-01-01
      • 2011-05-02
      • 1970-01-01
      • 2011-03-18
      • 2021-10-15
      • 1970-01-01
      • 2012-04-30
      相关资源
      最近更新 更多