-1

我正在做一个练习来实现一个无限滚动器,它从我设置的 MySQL 数据库中提取信息。我的滚动部分似乎可以工作,我的数据库、连接和查询在另一个文件中测试时似乎很好,而且我相信我的 XML 文件的结构是我想要的。但是,当我到达我尝试解析 XML、检索元素标签中的数据并将其附加到我的 html 的代码部分时,我在 Chrome 的检查器窗口中收到以下错误:

Uncaught Error: Syntax error, unrecognized expression: Resource id #2<?xml version="1.0"?>
<response><commentData>...(the rest of my XML file)

我对编程很陌生,对导致问题的原因感到茫然。下面是我的代码。我有一些东西被注释掉了,所以希望这不会让任何人失望。此外,include.inc.php 文件只包含用于连接数据库和执行查询的函数。谢谢。

AJAXscroller.html:

<!DOCTYPE html>
<html lang="en-US">

<head>
    <meta charset="utf-8" />
    <title>Scroller with AJAX!</title>

    <link rel="stylesheet" href="styles/scroller.css" type="text/css">

    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="./include/jquery.js"></script>
    <script src="./include/scroller.js"></script>
</head>

<body>
    <div id="info"></div>


    <section id="userComments">
        <p>Lorem ipsum dolor... </p> <!-- my code longer to fill page & requre scroll -->
            <p>Lorem ipsum dolor... tulla, semper gravida magna velit eu sem.</p>
            <p>Lorem ipsum dolor... tulla, semper gravida magna velit eu sem.</p>   
            <p>Lorem ipsum dolor... tulla, semper gravida magna velit eu sem.</p>   
            <p>Lorem ipsum dolor... tulla, semper gravida magna velit eu sem.</p>       
    </section>
    <div id="loaderGif"><img src="img/220.gif"></div>

</body>
</html>

scroller.js:

$(document).ready(
    function() 
    {
        $("#loaderGif").hide(); 
        var start = 0;     //variable to indicate the starting row in the db query
        var numRows = 30;  //this is the number of db rows to fetch with each query


        $(window).scroll(
            function()
            {
                var top = $(this).scrollTop();
                var txt = "scrollTop: " + top + 
                          "<br/>Win H: " + $(this).height() + 
                          "<br/>Page H: " + ($(this).height() + top) +
                          "<br/>Doc H: " + $(document).height();

                $("#info").html(txt);

            if ($(window).scrollTop() >= $(document).height() - $(window).height()) 
                {
                    $("#loaderGif").fadeIn(2000);
                    $.post("./include/AJAXscroll.php", {m: start, n: numRows}, function(xml) 
                       {
                           //code that happens when we get response

                           $("commentData", xml).each(
                               function()
                               {

                                   var userName = $(this).find("username").text();
                                   var comment = $(this).find("comment").text();
                                   var created = $(this).find("created").text();

                                   var str = "<hr><p>" + userName + " commented at " + created + "</p>" + comment;  

                                   $("#userComments").append(str);
                                   $("#loaderGif").fadeOut(2000);                          

                               }
                           );
                        }
                    ); 
                }  
            }
        );  
    }
);

AJAXscroll.php:

<?php

    include ("./include.inc.php");
    $link = connect();
    $db = "myTestDB";

    $m = $_POST["m"];
    $n = $_POST["n"];

    $sql = "SELECT * FROM comments WHERE username='Donna' ORDER BY created DESC LIMIT 0,30";
//  $sql = "SELECT * FROM comments WHERE username='Donna' ORDER BY created DESC LIMIT " . m . "," . n;
    $res = send_sql($sql, $link, $db);
    echo $res;

    $dom = new DOMDocument();
    $response = $dom->createElement("response");
    while ($row = mysql_fetch_assoc($res))         // create the XML file
    {
        $commentData = $dom->createElement("commentData");

        $username = $dom->createElement("username");
        $usernameValue = $dom->createTextNode($row["username"]);
        $username->appendChild($usernameValue);

        $comment = $dom->createElement("comment");
        $commentValue = $dom->createTextNode($row["comment"]);
        $comment->appendChild($commentValue);

        $created = $dom->createElement("created");
        $createdValue = $dom->createTextNode($row["created"]);
        $created->appendChild($createdValue);

        $commentData->appendChild($username);  
        $commentData->appendChild($comment);
        $commentData->appendChild($created);

        $response->appendChild($commentData);
     }  
     $dom->appendChild($response);
     $xmlString = $dom->saveXML();
//   $dom->save("myXML.xml");
     header('Content-type: text/xml'); 
     echo $xmlString;
?>
4

1 回答 1

0

您在 xml 数据之前回显内容,这会导致解析 xml 出现问题。删除echo $res;,您应该可以很好地解析 xml。

于 2013-03-10T19:13:40.363 回答