Php Ajax调用Joomla中的控制器?
好的,我为Joomla安装了一个组件,但现在我正在为添加自定义计数器视图而工作 在组件的controller.php中,我添加了以下函数:Php Ajax调用Joomla中的控制器?,php,jquery,ajax,joomla,Php,Jquery,Ajax,Joomla,好的,我为Joomla安装了一个组件,但现在我正在为添加自定义计数器视图而工作 在组件的controller.php中,我添加了以下函数: function updateView() { $db = JFactory::getDBO(); $videoid = JRequest::getVar('videoid'); if ($videoid) { $query = "update #__hdflv_upload SET times_viewed=
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>
$.ajax({
url:'index.php?option=com\u contushdvideoshare&task=updateView&format=raw',
成功:功能(数据){
如果(!data.length){
//出错
返回;
}
$('.container').html(数据);
}
});
有人能帮我吗?谢谢首先,您的第一个代码中存在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,这是一个好习惯
然后,缓存get查询。要改变这种行为,您有两种解决方案:
- Post请求从不缓存,因此请使用ajax Post请求
- 在ajax函数中指定不应缓存此请求
$.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);
}