dongluolie3487 2015-12-26 20:07
浏览 82

Ajax在Joomla中调用控制器?

Ok, I installed a component for my Joomla but now I'm working for add a custom counter views.

In controller.php of the component I added the following functions:

    function updateView() {
    $db = JFactory::getDBO();
    $videoid = JRequest::getVar('videoid');
    if ($videoid) {
        $query = "update #__hdflv_upload SET times_viewed=1+times_viewed where id=$videoid";
        $db->setQuery($query);
        $db->query();
    }
    $query = "select times_viewed from #__hdflv_upload where id=$videoid";
    $db->setQuery($query);
    $timeView = $db->loadResult();
    echo $timeView;
    jexit();
}

Anyway, in the page I tried to call this function with php and works fine, but with cache enabled the script stop working. They said to me that in Joomla the counter views should always be in Ajax. But I'm that expert on Ajax, I tried with this but with no result:

    <script>
$.ajax({
    url: 'index.php?option=com_contushdvideoshare&task=updateView&format=raw',
    success: function(data){
                if(!data.length){
            // Throw an error
            return;
        }

       $('.container').html(data);

    }
});
</script>

Can somebody help me? Thank you

  • 写回答

1条回答 默认 最新

  • dongyou2279 2015-12-29 08:35
    关注

    First of all you have a sql injection vulnerability in your first code it should be:

    function updateView() {
        $db = JFactory::getDBO();
        $videoid = JRequest::getInt('videoid');
        if ($videoid) {
            echo '0';
            jexit();
        }
        $query = "update #__hdflv_upload SET times_viewed=1+times_viewed where id=".(int)$videoid;
        $db->setQuery($query);
        $db->query();
    
        $query = "select times_viewed from #__hdflv_upload where id=".(int)$videoid;
        $db->setQuery($query);
        $timeView = $db->loadResult();
        echo $timeView;
        jexit();
    }
    

    Use JRequest::getInt('videoid') if you want to retreive an integer, Joomla will check and validate the input for you.

    You can also cast to int directly in your sql query which is a good habit.

    Then, get queries are cached. You have two solutions to change this behaviour :

    • Post requests are never cached so use an ajax post request
    • Specify in the ajax function that this request should not be cached

    Here is the js code modified

    $.ajax({
        url: 'index.php?option=com_contushdvideoshare&task=updateView&format=raw',
        method: "POST", //Use post method
        cache: false, //Specify no cache
        success: function(data){
            if(!data.length){
                // Throw an error
               return;
            }
           $('.container').html(data);
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流