[英]ionic 3 HttpClient error in connecting api, Property 'json' does not exist on type 'Object'
I am using Ionic 3, I am trying to connect to a api and my code is given below. 我正在使用Ionic 3,正在尝试连接到api,下面给出了我的代码。
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map'
@Injectable()
export class WeatherProvider {
apikey = 'xxxxxxxxxxxxxxx';
url;
constructor(public http: HttpClient) {
console.log('Hello WeatherProvider Provider');
this.url= 'http://api.wunderground.com/api/'+this.apikey+'/conditions/q';
}
getWeather(city, state){
return this.http.get(this.url+'/'+state+'/'+city+'.json')
.map(res => res.json());
}
}
I know the error, that is with this code .map(res => res.json()); 我知道错误,这是此代码。map(res => res.json());
Here is the error log 这是错误日志
Typescript Error Property 'json' does not exist on type 'Object'.
Typescript错误属性'json'在类型'Object'上不存在。
src/providers/weather/weather.ts return this.http.get(this.url+'/'+state+'/'+city+'.json') .map(res => res.json()); }
Ionic Framework: 3.9.2 Ionic App Scripts: 3.1.9 Angular Core: 5.2.10 Angular Compiler CLI: 5.2.10 Node: 9.2.1 OS Platform: Linux 4.13 Navigator Platform: Linux x86_64 User Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
Ionic框架:3.9.2 Ionic应用程序脚本:3.1.9 Angular Core:5.2.10 Angular编译器CLI:5.2.10节点:9.2.1 OS平台:Linux 4.13 Navigator平台:Linux x86_64用户代理:Mozilla / 5.0(X11; Linux x86_64)AppleWebKit / 537.36(KHTML,例如Gecko)Chrome / 63.0.3239.132 Safari / 537.36
Give me a solution please 请给我一个解决方案
You do not need to call .json()
with httpClient
because the response is JSON by default 您不需要使用
httpClient
调用httpClient
.json()
,因为默认情况下响应为JSON
return this.http.get(this.url+'/'+state+'/'+city+'.json')
.map(res => res);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.