创建天气预报最简单的方法是什么?

Whats the easiest way to create a weather-forecast?

本文关键字:是什么 方法 天气预报 最简单 创建      更新时间:2023-09-26

我正在开发一个web应用程序。这里我用JavaScript创建了一个弹出窗口。现在我想在这个窗口内创建一个天气预报。

最简单的方法是什么?

我是这样做的:用jQuery从雅虎获取天气

$(".popup-button").click(function(evt){
//prevent default link behavior
    evt.preventDefault();
//get id from clicked element
var id = $(this).attr("id");
switch (id) {
    case "popup-button-wetter":
        //centering with css
        centerPopup($("#popup-wrapper-wetter"));
        //load popup  
        loadPopupadditional($("#popup-wrapper-wetter"));
        //get weather
        getWeather ()
        break;
          ......

function getWeather () {
     $.get("http://weather.yahooapis.com/forecastrss?w=782458&u=c", function(data){
                   console.log("Data Loaded: " + data);
                 });
            }
    });

但是我得到了这个错误:

XMLHttpRequest无法加载http://weather.yahooapis.com/forecastrss?w=782458&u=c。Origin文件://Access-Control-Allow-Origin不允许。

这是什么意思?

我知道我迟到了,但是我在创建天气页面时遇到了同样的问题。我使用的是Google的API,但是你应该可以很容易地为Yahoo的API重写这个:

在PHP文件中执行如下操作:

<?php
$lat = explode(".", $_GET["lat"] * 1000000); //latitude returned by JS geolocation
$lon = explode(".", $_GET["lon"] * 1000000); //longitude returned by JS geolocation
$api = simplexml_load_string(utf8_encode(file_get_contents("http://www.google.com/ig/api?weather=,,," . $lat[0] . "," . $lon[0] . "&zoom=10&hl=de")));
echo $api->weather->current_conditions->temp_c->attributes()->data; //prints the current temperature in °C
?>

然后插入

将HTML页面的字符集设置为UTF-8
<meta charset="utf-8">

放到<head>标记中,以便正确显示像ä, ö和ü这样的变音符。

Tl;dr:您可以通过PHP抓取XML文件,然后向本地PHP文件发送XMLHttpRequest,从而绕过跨域AJAX块。div;)

不能使用javascript进行跨域ajax请求