【问题标题】:Ajax call to controller in Joomla?Ajax 调用 Joomla 中的控制器?
【发布时间】:2015-12-26 20:07:59
【问题描述】:

好的,我为我的 Joomla 安装了一个组件,但现在我正在努力添加自定义计数器视图。

在组件的controller.php中我添加了以下函数:

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

无论如何,在页面中我尝试使用 php 调用此函数并且工作正常,但启用缓存后脚本停止工作。他们对我说,在 Joomla 中,反视图应该始终在 Ajax 中。但我是 Ajax 方面的专家,我试过这个但没有结果:

    <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>

有人可以帮助我吗?谢谢

【问题讨论】:

    标签: php jquery ajax joomla


    【解决方案1】:

    首先,您的第一个代码中应该存在 sql 注入漏洞:

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

    如果您想检索一个整数,请使用 JRequest::getInt('videoid'),Joomla 将为您检查和验证输入。

    您也可以在 sql 查询中直接转换为 int,这是一个好习惯。

    然后,获取查询被缓存。您有两种解决方案来改变这种行为:

    • 从不缓存发布请求,因此请使用 ajax 发布请求
    • 在ajax函数中指定这个请求不应该被缓存

    这里是修改的js代码

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-10
      • 1970-01-01
      • 1970-01-01
      • 2018-04-19
      • 2021-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多