Angular Yahoo Weather API

时间:2018-06-30 08:28:53

标签: javascript angular api yahoo weather

我对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: 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);
  }
}

Result

2 个答案:

答案 0 :(得分:0)

工作示例:stackblitz

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

默认情况下,返回的数据为 XML 格式。

答案 1 :(得分:0)

这是一个简单的示例,但希望对您有所帮助。我将在这里使用名为axios的库:

const url = "https://query.yahooapis.com/v1/public/yql?q=select item.condition from weather.forecast where woeid in (select woeid from geo.places(1) where text='Sunderland') and u='c'&format=json";
  
    const getWeatherData = () => {
         axios.get(url)
        .then(res => console.log(res.data.query.results.channel.item.condition))
        .catch(err => console.log(err.data))
    }
    
    getWeatherData();
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.12.0/axios.min.js"></script>