jQuery自定义模板无法正常工作

时间:2012-07-09 21:35:06

标签: php javascript jquery html codeigniter

我在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));
}
}

有关为何发生这种情况的任何想法?

2 个答案:

答案 0 :(得分: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));

}

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

答案 1 :(得分:0)

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