JSON数据不会通过jQuerys getJSON()显示

P4t*_*4tR 2 javascript jquery json

以下代码应显示id我从服务器获取的JSON对象.我找不到错误,有人能帮帮我吗?先感谢您!

http://localhost:8387/nscalemc/rest/mon/resourcestatus.json调用时返回的JSON对象:

{
    "groupStatus": [
        {
            "id": "Application Layer Configuration-ApplicationLayer",
            "time": 1332755316976,
            "level": 0,
            "warningIds": [],
            "errorIds": []
        },
        {
            "id": "Application Layer-ApplicationLayer:nscalealinst2",
            "time": 1333431531046,
            "level": 0,
            "warningIds": [],
            "errorIds": []
        },
        {
            "id": "Application Layer-ApplicationLayer:nscalealinst1",
            "time": 1333431531046,
            "level": 1,
            "warningIds": [
                "documentarea.locked"
            ],
            "errorIds": []
        },
        {
            "id": "Storage Layer-StorageLayerInstance1",
            "time": 1331208543687,
            "level": 0,
            "warningIds": [],
            "errorIds": []
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我的HTML文件gadget.html:

<html>
<head>
    <title>Monitor</title>
    <link href="css/gadget.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="js/jquery-1.7.2.js"></script>
    <script type="text/javascript" src="js/gadget.js"></script>
</head>
<body>
    <div id="content"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我的JavaScript文件"gadget.js":

fetch_JSON();

function fetch_JSON() {
    var url = "http://localhost:8387/nscalemc/rest/mon/resourcestatus.json";

    $.getJSON(url+'?callback=?', function(data) {
        $(data.groupStatus).each(function() {
            $('#content').append('<p>ID: ' + $(this).id+ '</p>');
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

编辑: 谢谢你的解决方案!我通过Firebug调试并发现,getJSON调用以状态"401 unauthorized"结束.

Nic*_*tti 5

你应该做

    $('#content').append('<p>ID: ' + this.id+ '</p>');
Run Code Online (Sandbox Code Playgroud)

在这里摆弄http://jsfiddle.net/JaxpC/

编辑 - 当然你应该使用现成的处理程序来确保dom存在(我不认为那是你的情况,因为我有一个ajax调用,但更好的是确定

$(function() {
   var url = "http://localhost:8387/nscalemc/rest/mon/resourcestatus.json";

    $.getJSON(url+'?callback=?', function(data) {
        $(data.groupStatus).each(function() {
            $('#content').append('<p>ID: ' + this.id+ '</p>');
        });
    });
});
Run Code Online (Sandbox Code Playgroud)