简体   繁体   English

jQuery无法从JSON文件读取

[英]Jquery not reading from JSON file

Ok, so here is the code for my HTML: 好的,这是我的HTML的代码:

<div id="contentholder">
    <div class="personcard">
        <img src="http://placehold.it/400x400" />
        <h2>Matej Vydra <span>Striker</span></h2>
        <p>This is a paragraph about Watford Striker Matej Vydra, it doesnt really make much sense, but its great for testing and lolz.</p>
    </div>
</div>

and here is the JS and JSON files: 这是JS和JSON文件:

$(document).ready(function () {
    $.getJSON('json/currentsquad.json', function (currentsquad) {
        var output = "<div class='personcard'>";
        for (var i in currentsquad.players) {
            output += "<img src='" + currentsquad.players[i].image + "'/> <h2>" + currentsquad.players[i].name + "<span>" + currentsquad.players[i].position + "</span> </h2>";
        }
        output += "</div>";
        document.getElementById("contentholder").innerHTML = output;
    });
});

json: json:

{
    "players": [
        {
            "image": "http://placehold.it/400x400",
            "name": "Matej Vydra",
            "position": "Striker"
        },
        {
            "image": "http://placehold.it/400x400",
            "name": "Troy Deeney",
            "position": "Striker"
        },
]
}

And the problem is that the code is not getting shown to the user, I have tried searching previously and therefore tried running it on localhost but the issue stays the same and the JSON isn't shown to the user. 问题是代码没有显示给用户,我以前尝试搜索过,因此尝试在localhost上运行它,但问题仍然相同,并且没有向用户显示JSON。 Thanks in advance. 提前致谢。

Your JSON is invalid. 您的JSON无效。 While the JavaScript object literal notation allows for a trailing comma, the JSON standard does not. 尽管JavaScript对象文字符号允许使用逗号结尾,但JSON标准却不允许。 So if this is an accurate representation of the JSON you're sending, you'll need to remove the comma after the last object in the players Array. 因此,如果这是您要发送的JSON的准确表示,则需要删除players数组中最后一个对象之后的逗号。

{
    "players": [
        {
            "image": "http://placehold.it/400x400",
            "name": "Matej Vydra",
            "position": "Striker"
        },
        {
            "image": "http://placehold.it/400x400",
            "name": "Troy Deeney",
            "position": "Striker"
        }
    ]
}

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

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