1

このJSONからいくつかの要素を取得するために次のコードを使用していますが、何も機能していないようです。誰かが私が間違っていることを知っていますか?(私はそれを理解しようと長い時間を費やしました)。

$(document).ready(function() {

$.getJSON('http://free.worldweatheronline.com/feed/weather.ashx?q=Stockholm&format=json&num_of_days=2&key=e8536d3a52101433121710', function(Wdata) {
                $.each(Wdata.data, function() {

                $('<div id="test"></div>').append(

                        this.weather[0].date;

                        ).appendTo('body');

                  });
    });
});

私のHTMLは次のようになります。

<!doctype html>
<html lang="se">
    <head>
        <meta charset="utf-8" />
        <title>Title of This Web Page</title>
        <script src="scripts\jquery-1.8.2.js" type="text/javascript"></script>
        <script src="scripts\js.js" type="text/javascript"></script>
    </head>

    <body>
              <div id="test">
              </div>
    </body>

</html>

JSONは次のようになります。

{

    "data": {
        "current_condition": [ … ],
        "request": [ … ],
        "weather": [
            {
                "date": "2012-10-17",
                "precipMM": "0.8",
                "tempMaxC": "11",
                "tempMaxF": "51",
                "tempMinC": "8",
                "tempMinF": "46",
                "weatherCode": "119",
                "weatherDesc": [
                    {
                        "value": "Cloudy"
                    }
                ],
                "weatherIconUrl": [
                    {
                        "value": "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png"
                    }
                ],
                "winddir16Point": "SW",
                "winddirDegree": "224",
                "winddirection": "SW",
                "windspeedKmph": "27",
                "windspeedMiles": "17"
            },
            { … }
        ]
    }

}
4

2 に答える 2

0

リクエストがクロス ドメインの場合 = URL がドメインからのものではない場合は、クロス ドメイン フラグを設定するか、代わりに jsonp を使用する必要があります。

于 2012-10-18T08:28:56.530 に答える
0

JavaScript コンソールを使用します。それがあなたに与えるエラーを読んでください。

キャッチされていない SyntaxError: 予期しないトークンです。

式ではなくステートメントを終了します。から削除;しますthis.weather[0].date;

それを修正すると、次のようになります。

XMLHttpRequest を読み込めhttp://free.worldweatheronline.com/feed/weather.ashx?q=Stockholm&format=json&num_of_days=2&key=e8536d3a52101433121710. Origin http://fiddle.jshell.netませんが、Access-Control-Allow-Origin で許可されていません。

このサイトでよく取り上げられているもの

于 2012-10-18T08:28:09.043 に答える