简体   繁体   English

使用jQuery将JSON中的元素从html获取

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

I'm using following code to get some elements out of this JSON, but nothing seems to work. 我正在使用以下代码从JSON中获取一些元素,但似乎没有任何效果。 Anybody has an idea of what I'm doing wrong? 有人知道我在做什么错吗? (I've spent a long time trying to figure it out). (我花了很长时间试图弄清楚)。

$(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');

                  });
    });
});

my HTML looks like this: 我的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>

The JSON looks like this: 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"
            },
            { … }
        ]
    }

}

Use the JavaScript console. 使用JavaScript控制台。 Read the errors it gives you. 阅读它给您的错误。

Uncaught SyntaxError: Unexpected token ; 未捕获到的SyntaxError:意外令牌;

Terminate statements, not expressions. 终止语句,而不是表达式。 Remove the ; 删除; from this.weather[0].date; this.weather[0].date;

If you fix that then you get: 如果解决此问题,您将获得:

XMLHttpRequest cannot load http://free.worldweatheronline.com/feed/weather.ashx?q=Stockholm&format=json&num_of_days=2&key=e8536d3a52101433121710. Origin http://fiddle.jshell.net XMLHttpRequest无法加载http://free.worldweatheronline.com/feed/weather.ashx?q=Stockholm&format=json&num_of_days=2&key=e8536d3a52101433121710. Origin http://fiddle.jshell.net http://free.worldweatheronline.com/feed/weather.ashx?q=Stockholm&format=json&num_of_days=2&key=e8536d3a52101433121710. Origin http://fiddle.jshell.net is not allowed by Access-Control-Allow-Origin. 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

Which is well covered on this site 该网站上涵盖了哪些内容

如果您的请求是跨域=网址不是来自您的域,则应设置跨域标志或改用jsonp。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM