0
我正在嘗試使用開放天氣地圖api製作Angular 2中的天氣應用程序。我的應用程序中安裝了HttpModule,位於app.module.ts中。我也在app.component.ts中導入了Http,我正在運行該程序。無法訪問Angular 2中的JSON對象?
例如,我想獲取休斯頓天氣的json數據。我的代碼如下:
import { Component, OnInit } from '@angular/core';
import { Http } from '@angular/http'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
newdata;
constructor (private http: Http) {}
ngOnInit(): void{
this.http.get('http://api.openweathermap.org/data/2.5/weather?q=houston&units=imperial&APPID=hiddenforstackoverflow').subscribe(data => {
this.newdata= data
})
}
getData(){
console.log(this.newdata)
}
但是,運行getData()函數後,控制檯並未真正顯示出我的預期。
headers:Headers {_headers: Map(1), _normalizedNames: Map(1)}
ok:true
status:200
statusText:"OK"
type:2
url:"http://api.openweathermap.org/data/2.5/weather?q=houston&units=imperial&APPID=d3758344ecd26680d1ae2845dcfbbaf7"
_body:"{"coord":{"lon":-95.36,"lat":29.76},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"base":"stations","main":{"temp":74.17,"pressure":1014,"humidity":83,"temp_min":73.4,"temp_max":75.2},"visibility":16093,"wind":{"speed":5.82},"clouds":{"all":75},"dt":1508550780,"sys":{"type":1,"id":2638,"message":0.1571,"country":"US","sunrise":1508588841,"sunset":1508629448},"id":4699066,"name":"Houston","cod":200}"
__proto__:Body
我需要訪問內部_body(如座標,溫度等)的參數但是,使用data._body不起作用。 我是Angular的新手,習慣於在香草javascript中調用API,我使用ajax方法。