Angular-http.get响应不会加载所有数据

时间:2018-07-01 22:04:25

标签: angular entity-framework angular5 asp.net-core-2.0

当我用POSTMAN发送http.get请求时,我正在服务器端使用ASP Core 2,客户端使用Angular 5的项目。

POSTMAN OPERATION GET

问题是,当我以角度5发送相同的请求时,并非所有数据都已加载(检查,recRes)

Not All the Data loaded

这是我使用ASP Core 2的服务器代码

 // GET: api/Reclamations/5
    [HttpGet("byClient/{id}"), Authorize]
    public async Task< ActionResult > Get(int id)
    {
        try
        {
            var ResRecs = await _context.Reclamations
                .Where(r => r.IdClient == id)
                .Include(n => n.IdClientNavigation)
                .Select(x => new
                {
                    observations = x.Observations,
                    rdv = x.Rdv,
                    explications = x.Explication,
                    idRec = x.Id,
                    pattern = x.Pattern

                })
                .OrderBy(t => t.rdv)
                .ToListAsync();
            var checkBoxes = new List<String>();
            foreach(var item in ResRecs)
            {
                if (item.pattern.Contains("DOTDelanteroIzquierdo")) checkBoxes.Add("DOTDelanteroIzquierdo"+item.idRec);
                if (item.pattern.Contains("DOTDelanteroDerecho")) checkBoxes.Add("DOTDelanteroDerecho" + item.idRec);
                if (item.pattern.Contains("DOTTraseroIzquierdo")) checkBoxes.Add("DOTTraseroIzquierdo" + item.idRec);
                if (item.pattern.Contains("DOTTraseroDerecho")) checkBoxes.Add("DOTTraseroDerecho" + item.idRec);
            }

            var  ResClient = await _context.Clients
               .Where(r => r.Id == id)
               .Select(x => new
               {
                   idClient = x.Id,
                   nameClient = x.FullName,
                   addressClient = x.Address,
                   postalCodeClient = x.PostalCode,
                   cityClient = x.City,
                   telClient = x.Tel,
                   emailClient = x.Email
               }).SingleAsync();
            return Content(Newtonsoft.Json.JsonConvert.SerializeObject(new {ResClient, ResRec = ResRecs, Check = checkBoxes , success = true, message = "The Reclamation loaded"  }), "application/json");


        }
        catch (Exception ex)
        {
            Console.WriteLine("Error : " + ex);
        }
        return Content(Newtonsoft.Json.JsonConvert.SerializeObject(new { success = false, message = "The Reclamation is not loaded" }), "application/json");

    }

这是我的Angular 5通话服务

import { Injectable } from '@angular/core';
import { AppconfigService } from '../appconfig.service';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class ReclamationsService {

    constructor(private http: HttpClient) { }
     UrlRecs: string = AppconfigService.settings.server.url + "api/Reclamations/";
    getRec(idClient){

    return this.http.get(this.UrlRecs + "byClient/" + idClient);
}
}

这是我对服务的组件调用

this.reclamationSvc.getRec(this.idClient).subscribe(
  (response: Response) => {
    console.log('Before', response);
    if (response["success"] == true) {
       this.ResRecs = response["ResRec"];

      this.checkBoxs = response["Check"];

      this.c = response["ResClient"];


    }
    else {
      alert("Error");
    }

  },
  (error) => {

  }
)

我已经尝试了5天,我的代码可能出什么问题了?

1 个答案:

答案 0 :(得分:0)

我不知道。但是尝试API返回OK

return OK(new {ResClient, ResRec = ResRecs, Check = checkBoxes , 
               success = true, message = "The Reclamation loaded"  });

并在Angular中使用点表示法

   if (response.success == true) {
      this.ResRecs = response.ResRec;
      this.checkBoxs = response.Check;
      this.c = response.ResClient;
    }