Javascript 天气预报API

Javascript 天气预报API,javascript,angular,api,yahoo,weather,Javascript,Angular,Api,Yahoo,Weather,我对雅虎天气API有问题。我应该如何请求从Yahoo Weather API获取数据 import { Injectable } from '@angular/core'; import {Observable} from 'rxjs'; import {HttpClient} from '@angular/common/http'; @Injectable() export class WeatherService { constructor(private http: HttpClie

我对雅虎天气API有问题。我应该如何请求从Yahoo Weather API获取数据

import { Injectable } from '@angular/core';
import {Observable} from 'rxjs';
import {HttpClient} from '@angular/common/http';

@Injectable()
export class WeatherService {
  constructor(private http: HttpClient) { }

  getWeatherForecast(city: string): Observable<any> {
   const url = 'https://query.yahooapis.com/v1/public/yql?q=select wind from 
   weather.forecast where woeid=2460286';
   return this.http.get(url);
  }
}
工作示例:

您唯一的问题是忘记将:&format=json添加到url字符串中


返回的数据默认为XML格式。

这是一个简单的示例,但我希望它能帮助您。我将在此处使用名为axios的库:

常量url=https://query.yahooapis.com/v1/public/yql?q=select weather.forecast中的item.condition,其中woeid在geo.places1中选择woeid,其中text='Sunderland'和u='c'&format=json; 常量getWeatherData==>{ axios.geturl .thenres=>console.logres.data.query.results.channel.item.condition .catcherr=>console.logerr.data } 获取天气数据;
请用实际代码而不是图片编辑您的问题。谢谢你,你现在得到的是404,可能是因为URL中有空格。您可以尝试在该url上使用encodeURI,并使用已安装的编码url吗?另外,当我点击那个URL时,我会返回XML,这可能会给您带来问题,由于HttpClient将尝试按默认值将结果解析为JSON,我已将*%20的url从%20weather.forecast%20where%20woeid%20in%20select%20woeid%20from%20geo.places1%20where%20text%3D%22greenland%22&format=JSON&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys from docs,但结果是一样的。如果你在浏览器的开发者工具网络面板中检查,它是否试图连接到正确的URL?那个URL对我来说似乎很好用?如果不行,你能创建一个复制问题的StackBlitz吗?谢谢你说得对。我在应用程序中有内存中的web api。我认为这是一个问题。