代码之家  ›  专栏  ›  技术社区  ›  Uahmed

Angular6 Get method response“\u isScalar”:false,“源”

  •  0
  • Uahmed  · 技术社区  · 8 年前

    {“{u isScalar”:false,“source”:{“{u isScalar”:false,“source”:{“{u isScalar”:false,“source”:{“{u isScalar”:true,“value”:{“url”: https://feeds.citibikenyc.com/stations/stations.json “,”body“:null,”reportProgress“:false,”withCredentials“:false,”responseType“:”json“,”method“:”GET“,”headers“:{”normalizedNames“:{}”,lazyUpdate“:null”,headers“:{}”,params“:{”updates“:{”null,“cloneFrom“:null”,encoder“:{}”,map“:null}”,urlWithParams“:” https://feeds.citibikenyc.com/stations/stations.json “}”,scheduler“:null}”,operator“{”concurrent“:1}}”,operator“{}”,operator“{}}”

    我获取数据的代码是

    import { HttpClient} from '@angular/common/http';
    import { Injectable } from '@angular/core';
    
    @Injectable()
    export class GetdataService {
    posts : any;
    readonly ROOT_URL ="https://feeds.citibikenyc.com/stations/stations.json";
    constructor(private http: HttpClient ) { }
    
    getPosts()
    {
    
    this.posts = this.http.get(this.ROOT_URL );
    
    return JSON.stringify(this.posts);
    }
    
    
    }
    
    1 回复  |  直到 8 年前
        1
  •  7
  •   vicbyte    8 年前

    this.http.get() 不返回数据,而是要订阅的可观察数据:

    getPosts() {
      this.http.get(this.ROOT_URL)
        .subscribe((data) => {
            //DO STUFF HERE
        });
    }
    

    Readmore

    其次,虽然在修复了可观察的内容之后不需要对JSON进行解码,但是可以使用 JSON.parse() 不 JSON.stringify stringify 将对象转换为字符串(与所需完全相反)。

    推荐文章