我正在为我的演示应用程序(https://restcountries.eu/)使用此api
API url是这个
https://restcountries.eu/rest/v2/all
但是我无法从角度获取服务中的数据
这是我的代码
https://stackblitz.com/edit/angular-3jpqrc?file=src%2Fapp%2Fapp.component.ts
constructor(private dataservice : DataService){
this.dataservice.fetchDataThroughPromise().subscribe(()=>{
console.log('s')
})
}
服务代码
fetchDataThroughPromise(){
return this.http.get('https://restcountries.eu/rest/v2/all',
{
headers :new HttpHeaders({
'content-type': 'application/json'
})
});
}
使用angular cli创建项目时看到错误
最佳答案
只需删除:
headers :new HttpHeaders({
'content-type': 'application/json'
})
最终代码:
fetchDataThroughPromise() {
return this.http.get('https://restcountries.eu/rest/v2/all')
}