简体   繁体   English

http.get返回什么以及如何将其转换为对象

[英]What does http.get return and how to convert it to an object

I have this method in my component 我的组件中有这种方法

  GetInfo(id)
 {
  const data = this.http.get(this.baseUrl + "api/SampleData/EditMake/" + id);
  console.log(data);
 }

It just runs a http.get to a web api controller method. 它只是运行http.get到Web api控制器方法。 Which returns an object. 返回一个对象。

    [HttpGet]
    [Route("api/[Controller]/EditMake/{id:int}")]
    public IActionResult EditMake(int id)
    {
        return Ok(_vehicleService.GetMakeById(id));
    }

That is the method in question. 那是有问题的方法。 Problem is, data isn't an object of the type which the EditMake method returns. 问题是,数据不是EditMake方法返回的类型的对象。 What am i missing? 我想念什么?

Need to subscribe to the HTTP request. 需要订阅HTTP请求。 Http request return an observable as a response and in order to access the data, we need to subscribe to the observable. Http请求返回一个可观察对象作为响应,为了访问数据,我们需要订阅该可观察对象。

  GetInfo(id) {

   let data; 

   this.http.get(this.baseUrl + "api/SampleData/EditMake/" + id).subscribe((response) => {
        data = response;
        console.log(data);

   });
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM