Angular Yahoo Weather API - PullRequest
       31

Angular Yahoo Weather API

0 голосов
/ 30 июня 2018

У меня проблема с API погоды Yahoo. Как мне сделать запрос на получение данных от 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 голосов
/ 30 июня 2018

Это простой пример, но я надеюсь, что он вам поможет. Я буду использовать библиотеку под названием 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>
0 голосов
/ 30 июня 2018

Рабочий пример: stackblitz

Ваша единственная проблема заключалась в том, что вы забыли добавить: & format = json в строку URL.

Возвращенные данные были в формате XML по умолчанию.

...