0

我有这个 HTML 代码:

<tr class="gris">
    <td width="6%" class="titulo"><a href="{{ Here goes some URL}}"><img src="assets/play_icon.png" width="32" height="32" /></a></td>
    <td width="60%" class="titulo"><a href="{{ Here goes some URL }}">{{ Here goes some title }}</a> <span></br>Descripción de la Pista o Podcast</span></td>
    <td width="17%" class="fecha">{{ Here goes some date}}</td>
    <td width="17%" class="tiempo">{{ Here goes some time}}</td>
</tr>

我有一个返回 JSON 的 PHP 代码,如下所示:

{ "html_content" : [ { "description" : "Esta es una prueba de grabacion del podcast",
        "date" : "20130927",
        "hour" : "012100",
        "id" : "-317498614",
        "repro" : "0",
        "title" : "Prueba",
        "url" : "rtmp://46.4.158.7/kraa13/_definst_/kraa13/prueba1.flv"
      },
      { "description" : "260913",
        "date" : "20130926",
        "hour" : "192600",
        "id" : "-317498614",
        "repro" : "0",
        "title" : "260913",
        "url" : "rtmp://127.0.0.1/kraa13/_definst_/kraa13-317498614/260913.flv"
      }
    ],
  "response" : true
}

我应该能够TR在 JSON 上生成尽可能多的值,当然还可以替换生成的正确值TR。例如,对于我在示例代码中的每次迭代,我应该从 JSON 响应中{{ Here goes some URL }}写入值。url

更新

我试过这段代码:

$(function() {
    window.setInterval(function() {
        var request = $.ajax({
            type: 'GET',
            url: "http://devserver/reader/podcast/podcast.php",
            success: function(data) {
                $("#podlist").html();
                if (data.response === false) {
                    $("#podlist").html(data.error);
                } else {
                    console.log(data.html_content);
                    if (data.html_content.length != 0) {
                        var htm = null;
                        for (var i in data.html_content) {
                            var jsonObj = data.html_content[i];
                            htm += "<tr class='gris'><td width='6%' class='titulo'><a href='" + jsonObj.url + "'><img src='assets/play_icon.png' width='32' height='32' /></a></td><td width='60%' class='titulo'><a href='" + jsonObj.url + "'>" + jsonObj.title + "</a> <span></br>" + jsonObj.description + "</span></td><td width='17%' class='fecha'>" + jsonObj.date + "</td><td width='17%' class='tiempo'>" + jsonObj.hour + "</td></tr>";
                        }
                        $("#podlist").append(htm);
                    }
                }
            },
            error: function() {
                request.abort();
            }
        });
    }, 5000);
});

但我收到了这个错误:

TypeError: data.html_content is undefined
if (data.html_content.length != 0) {
4

6 回答 6

1

您可以尝试获取 table 元素,清除它的内部 HTML,然后在 $.each 循环中为您的响应(它是一个列表)附加所需的行。如果“行”计数是动态的并且您只有在异步请求之后才知道它是 imo 最好的方法。

于 2013-09-28T16:49:13.003 回答
1

希望您使用AJAX动态地谈论“从 JSON 结果更新 HTML 内容” ,我提出了以下解决方案。只需在 AJAX 的成功回调中编写此代码:

if(responseData.html_content.length != 0) {
  var htm = null;
  for(var i in responseData.html_content) {
      var jsonObj = responseData.html_content[i];
      htm += "<tr class='gris'><td width='6%' class='titulo'><a href='"+jsonObj.url+"'><img src='assets/play_icon.png' width='32' height='32' /></a></td><td width='60%' class='titulo'><a href='"+jsonObj.url+"'>"+jsonObj.title+"</a> <span></br>"+jsonObj.description+"</span></td><td width='17%' class='fecha'>"+jsonObj.date+"</td><td width='17%' class='tiempo'>"+jsonObj.hour+"</td></tr>";
  }
  $("#table-id").append(htm);
}

希望这可以帮助!

于 2013-09-28T20:02:18.187 回答
0

只需使用

var myhtml = '<tr class="gris"><td width="6%" class="titulo"><a href="{{ Here goes some URL}}"<img src="assets/play_icon.png" width="32" height="32" /></a></td><td width="60%" class="titulo"><a href="{{ Here goes some URL }}">{{ Here goes some title }</a><span></br>Descripción de la Pista o Podcast</span></td><td width="17%" class="fecha">{{ Here goes some date}}</td><td width="17%" class="tiempo">{{ Here goes some time}}</td></tr>';
$.each(the_data,function(i , v) {
    $('.titulo a' , myhtml).attr('href' , v.url);
    // and so on
    $('.conatiner').append(myhtml);
})
于 2013-09-28T16:48:45.973 回答
0

它类似于这种格式。你必须做一些工作。:)

$.post('test.php', { id: id },
  function (page) {
    $('.titulo a' , myhtml).attr('href' , page.html_content..url);
    $('.conatiner').append(page.html_content.data);
}, 'json');

从这里检查答案.. 如何从 json 获取参数?

于 2013-09-28T17:14:53.880 回答
0

简单的解决方案是将模板维护为

var template = '<tr class="gris">'+
    '<td width="6%" class="titulo"><a href="{{ Here goes some URL}}"><img src="assets/play_icon.png" width="32" height="32" /></a></td>'+
    '<td width="60%" class="titulo"><a href="{{ Here goes some URL }}">{{ Here goes some title }}</a> <span></br>Descripción de la Pista o Podcast</span></td>'+
    '<td width="17%" class="fecha">{{ Here goes some date}}</td>'+
    '<td width="17%" class="tiempo">{{ Here goes some time}}</td>'+
'</tr>';

var jstring = jQuery.parseJSON(jsonString);

var htmlContent = jstring.html_content;
var i=0;
var t =template;
for(;i<htmlContent.length;i++){
    t = t.replace("{{ Here goes some URL}}", htmlContent[i].url);   
    //Similarly  do for the other values...
    jQuery("table").append(jQuery(t));
    t=template;
}
于 2013-09-28T17:18:26.943 回答
-2

尝试做

控制台.log(数据);

Ajax 调用是否返回数据?

f12 并设置一个断点或使用您选择的浏览器中的“net”面板来查看您的 Ajax 调用返回的内容。

于 2013-09-28T16:49:32.617 回答