0

我在 Javascript/jQuery 中有一个自定义模板设置,我需要从 CodeIgniter 控制器中提取数据并将返回的 JSON 插入到 js/jQuery 模板中。我确实相信我的逻辑是正确的,但由于某种原因似乎没有任何效果,并且我在脚本的开头收到以下错误:

未捕获的 SyntaxError:输入意外结束

我该怎么做呢?到目前为止,我编写的代码如下所示:

$("#projects").click(function () {
    jQuery.ajax({
        type: "POST",
        dataType: "JSON",
        url: "<?=base_url()?>index.php/home/projectsSlider",
        data: dataString,
        json: {
            returned: true
        },
        success: function (data) {
            if (data.returned == true) {
                $("#content").fadeOut(150, function () {
                    $(this).replaceWith(projectsSlider(), function () {
                        var html = projectsSlider(data.projectId, data.projectName, data.startDate, data.finishedDate, data.createdFor, data.contributors, data.screenshotURI, data.websiteURL);
                        jQuery(html).appendTo("#content").fadeIn();
                    });
                });
            }
        }
    });
});

这是我的PHP:

function projectsSlider() {
    $query  = $this->db->query("SELECT * FROM projects ORDER BY idprojects DESC");
    foreach ($query->result() as $row) {
        $projectId = $row->projectId;
        $projectName = $row->projectName;
        $startDate = $row->startDate;
        $finishedDate = $row->finishedDate;
        $createdFor = $row->createdFor;
        $contributors = $row->contributors;
        $projectDesc = $row->projectDesc;
    }
    $query1 = $this->db->query("SELECT * FROM screenshots s WHERE s.projectId = '{$projectId}' ORDER BY s.idscreenshot DESC");
    foreach ($query1->result() as $row2) {
        $screenshotURI = $row2->screenshotURI;
        $websiteURL = $row->websiteURL;
    }
    echo json_encode(array('returned' => true,
        'projectId' => $projectId,
        'projectName' => $projectName,
        'startDate' => $startDate,
        'finishedDate' => $finishedDate,
        'projectDesc' => $projectDesc,
        'createdFor' => $createdFor,
        'contributors' => $contributors,
        'screenshotURI' => $screenshotURI,
        'websiteURL' => $websiteURL));
}
}

关于为什么会发生这种情况的任何想法?

4

2 回答 2

2

问题看起来像你有一个尾随}

编辑:在我重构时有人回答了,但我还是提供了这个版本:

function projectsSlider() {

    $query  = $this->db->query("SELECT * FROM projects ORDER BY idprojects DESC");
    $project = $query->fetch(PDO::FETCH_OBJECT);
    $project->screenshots = array();

    $query = $this->db->query("SELECT * FROM screenshots WHERE projectId = '$projectId' ORDER BY idscreenshot DESC");

    foreach ($query->result() as $screenshot) {
        $project->screenshots[] = $screenshot;
    }
    echo json_encode(array('returned' => true,'project'=>$project));

}

由于您可以取而代之的是获取对象,因此您不必进行所有循环和转换。

于 2012-07-09T21:49:31.510 回答
0

语法错误与您的代码逻辑无关。这意味着您的语法错误。}如果那是您唯一的 PHP 代码,那么它正在发生,因为您在函数末尾有一个额外的代码。您应该查看 PHP linter 以避免这些挂断。

于 2012-07-09T21:42:31.277 回答