[英]Angular6 Get method response “_isScalar”:false,“source”
我正在嘗試在html頁面上顯示json數據。 服務器上的數據向我顯示json數據,但是當我嘗試在頁面上顯示它時,它為我提供了此數據
{“ _isScalar”:false,“源”:{“ _ isScalar”:false,“源”:{“ _ isScalar”:false,“源”:{“ _ isScalar”:true,“值”:{“ url”:“ https://feeds.citibikenyc.com/stations/stations.json “,” body“:null,” reportProgress“:false,” withCredentials“:false,” responseType“:” json“,” method“:” GET“ ,“標頭”:{“ normalizedNames”:{},“ lazyUpdate”:null,“標頭”:{}},“ params”:{“ updates”:null,“ cloneFrom”:null,“ encoder”:{} ,“ map”:null},“ urlWithParams”:“ https://feeds.citibikenyc.com/stations/stations.json ”}“,” scheduler“:null},” operator“:{” concurrent“:1}} ,“操作員”:{}},“操作員”:{}}
我獲取數據的代碼是
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);
}
}
this.http.get()
不返回數據,而是可觀察的,必須訂閱:
getPosts() {
this.http.get(this.ROOT_URL)
.subscribe((data) => {
//DO STUFF HERE
});
}
其次,雖然在修復observable-stuff之后不需要解碼JSON,但您可以使用JSON.parse()
而不是JSON.stringify
來解碼JSON。 stringify
將對象轉換為字符串(與您想要的相反)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.