AJAX响应没有附加到JQuery

时间:2016-03-19 20:01:30

标签: javascript php jquery ajax append

我希望有人可以帮助我。我不是非常使用JQuery或javascript - 我更像是一个php人。我有一些似乎有效的代码,除了用JQuery实际将内容附加到UL。如果我在firebug中查看日志,我会看到它带回正确的数据,但数据实际上从未放入页面中。我试过.html,.load和.append但没有运气。它将始终从.load中的php脚本加载初始HTML,但是即使数据始终相同,它仍然是.append或.html是否有效。这就是我所拥有的:

$(document).ready(function(){
        var page = 1;

        $('#favoritesul').load("/custom/getfavorites.php"); //initial load

        //load more on click
        $('#favoritesul').on( "click", "a.paginated", function (e) {
            e.preventDefault();
            ++page; //increment page counter
            //temporarily hide the load icon
            $('#favoritesul > a.paginated').remove();
            //send AJAX
            $.ajax({
                type: 'POST',
                dataType: 'html',
                url: '/custom/getfavorites.php',
                data: { page: page}
            })
            .done(function(data) {
                $('#favoritesul').append(data);
                $('#favoritesul').append('<a href="#" class="paginated"><i class="icon-undo"></i>LOAD MORE</a></ul>');
            })
            .fail(function(){
                alert('post failed');
            });
        });
    });

正如我所说,如果我是console.log并查看AJAX响应,我会看到我期望的HTML,它不会附加甚至替换HTML的内容。为清楚起见,我试图追加的HTML内容如下所示:

<li class='data-item'><div class='test'><div class='padding'><div class='image-container'><a href='pagelink.php'><img class='image' src='imagesource.jpg' /></a></div><div class='header'><h2 class='heading'><a href='pagelink.php'>Video</a></h2></div></div></div></li>

现有页面的相关部分在第一个.load事件之前看起来像这样:

<div class='clear'></div><ul id='favoritesul' class='someclasses'></ul>

提前感谢所有帮助!

1 个答案:

答案 0 :(得分:0)

您的done()处理程序中确实存在无效的HTML。首先#favoritesul是UL,你不能附加一个锚点,只能附加一个LI,其次,你不能附加部分元素,就像你试图通过只包括结束{{1标签。

尝试附加有效的HTML

</ul>