1

Im developing an web app. Here I created a popup-window in JavaScript. Now I like to create inside this window a weather-forecast.

What is the easiest way to do this?

I did it like here: Get weather from Yahoo with 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);
                 });
            }
    });

but then I got this error:

XMLHttpRequest cannot load http://weather.yahooapis.com/forecastrss?w=782458&u=c. Origin file:// is not allowed by Access-Control-Allow-Origin.

What does it mean?

4

2 に答える 2

1

遅れていることはわかっていますが、天気ページを作成しているときに同じ問題に遭遇しました。私は 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 ブロックをバイパスできます。;)

于 2012-01-23T20:34:23.550 に答える
1

JavaScript を使用してクロスドメインの ajax リクエストを行うことはできません。

于 2012-01-03T14:39:34.197 に答える