[英]Ajax call to controller in Joomla?
好的,我為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>
有人可以幫我嗎? 謝謝
首先,您的第一個代碼中有一個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,這是一個好習慣。
然后,獲取查詢被緩存。 您有兩種解決方案來更改此行為:
這是修改后的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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.