简体   繁体   中英

Angular Yahoo Weather API

I have a problem with the Yahoo Weather API. How should I make a request to get data from the 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);
  }
}

结果

Working example: stackblitz

Your only problem was that you forgot to add: &format=json to your url string.

The returned data was in XML format by default.

This is a simple example, but I hope it helps you. I'll use a library called axios right here:

 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM