首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular Yahoo天气API

Angular Yahoo天气API
EN

Stack Overflow用户
提问于 2018-06-30 16:28:53
回答 2查看 1.1K关注 0票数 0

我对Yahoo Weather API有一个问题。我应该如何请求从Yahoo Weather API获取数据?

代码语言:javascript
复制
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);
  }
}

EN

回答 2

Stack Overflow用户

发布于 2018-06-30 16:56:57

工作示例:stackblitz

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

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

票数 0
EN

Stack Overflow用户

发布于 2018-06-30 17:53:59

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

代码语言:javascript
复制
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();
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.12.0/axios.min.js"></script>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51113184

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档