duancaozen6066 2012-07-09 21:35
浏览 33
已采纳

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

I have a custom template setup in Javascript/jQuery and I'm needing to pull data from a CodeIgniter controller and insert the returned JSON into the js/jQuery template. I do believe my logic is correct but for some reason nothing seems to work and I'm getting the following error in the beginning of my script:

Uncaught SyntaxError: Unexpected end of input

How would I go about doing this? The code I have written so far is shown below:

$("#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();
                    });
                });
            }
        }
    });
});

Here is my 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));
}
}

Any ideas as to why this is happening?

  • 写回答

2条回答 默认 最新

  • doujian0265 2012-07-09 21:49
    关注

    The problem looks like you've got a trailing }

    edit: Someone else answered while I was refactoring, but I offer this version anyway:

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

    Since you can fetch an object instead, you don't have to do all that looping and converting.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。