簡體   English   中英

使用jQuery將JSON中的元素從html獲取

[英]Getting elements from JSON into html with jQuery not working

我正在使用以下代碼從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"
            },
            { … }
        ]
    }

}

使用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不允許使用http://free.worldweatheronline.com/feed/weather.ashx?q=Stockholm&format=json&num_of_days=2&key=e8536d3a52101433121710. Origin http://fiddle.jshell.net

該網站上涵蓋了哪些內容

如果您的請求是跨域=網址不是來自您的域,則應設置跨域標志或改用jsonp。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM