我正在尝试从json文件获取内容,但是直到现在我什么都没得到。

我的状态连接== 200,可以在chrome控制台中看到内容
但是当我尝试将数据显示到html表时我什么也没得到,但是当我将相同的jquery代码与其他服务(例如import.io)的api一起使用时,一切正常。

你能告诉我我在做什么错吗?
该API来自kimonolabs。

$(document).ready(function () {

    var tabel = '<table><THEAD><caption>Calendário</caption></THEAD>';
    tabel += '<th>' + 'Hora' + '</th>' + '<th>' + 'Equipas' + '</th><th>' + 'jornda' +
        '</th><th>' + 'Data' + '</th>';

    $.ajax({
        type: 'GET',
        url: 'https://api.myjson.com/bins/1dm6b',
        dataType: 'json',
        success: function (data) {
            console.log(data);

            $('#update').empty();

            $(data.m_Marcadores).each(function (index, value) {
                tabel += '<tr><td>' + this.posicao + '</td>' + '<td>' + this.golos + '</td></tr>';
            }); //each

            tabel += '</table>';

            $("#update").html(tabel);

        } //data

    }); //ajax
}); //ready

最佳答案

根据JSON结构,您应该遍历data.results.m_Marcadores数组:

$(data.results.m_Marcadores).each(function (index, value) {
    tabel += '<tr><td>' + this.posicao + '</td><td>' + this.golos + '</td></tr>';
});


另一个问题。在表的标题中,您设置了4个列,但在循环中,您仅创建了其中两个。标头列数应与其他行td相同。

另外,您需要将th元素包装在tr中。例如,固定表头:

var tabel = '<table>' +
            '<THEAD><caption>Calendário</caption></THEAD>' +
            '<tr>' +
                '<th>Hora</th><th>Equipas</th><th>jornda</th><th>Data</th>' +
            '</tr>';


演示:http://jsfiddle.net/onz02e43/

关于javascript - 显示json文件中的数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29185959/

10-17 02:37