[英]Joomla and AJAX
我正在努力讓我的Joomla 3.6組件中的ajax調用正常工作。 下面的javascript函數工作的意思是它確實返回數據並在頁面中顯示它 - 一瞬間 - 然后頁面重新加載,我得到view.html.php的結果,這不是我的需要在這里。 無論我將async屬性設置為true還是false,我都得到相同的結果。 異步設置為true時,它發生得更快。 view.raw.php中返回的數據很好。 我必須避免重新加載view.html.php。 javascript函數getCase()由按鈕的“onclick”調用,我總是在測試之前清除緩存。 謝謝。
function getCase(getDataVal)
{
var examcaseid = ($('examcase_id')) ? $('examcase_id').value : -1;
jQuery.ajax({
type: 'GET',
url: 'index.php?option=com_casecreator&view=examcase&format=raw&layout=edit&id='+examcaseid+'&getdata='+getDataVal,
dataType: 'html',
contentType: 'text/html; charset=\"utf-8\"',
async: true,
success: function (data, status, jq) {
$('rc_xml_display').innerHTML = data;
//alert('In getCase(), rc_xml_display is: '+$('rc_xml_display').innerHTML);
},
error: function (jq, status, e) {
alert('Unable to retrieve case.'+JSON.stringify(jq));
}
});
return;
}
來自view.raw.php的整個display()函數:
public function display($tpl = null)
{
$app = JFactory::getApplication();
$getdata = $app->input->get('getdata');
$case_index = !empty($app->getUserState('case_index')) ? $app->getUserState('case_index') : 0;
if ($getdata == 'nextcase' || $getdata == 'previouscase' || $getdata == 'initialcase') {
$app->setUserState('active_tab', 'browsecases');
$cases_arr = $app->getUserState('rc_cases');
switch ($getdata) {
case 'nextcase': $case_index++; break;
case 'previouscase': $case_index--; break;
case 'initialcase': $case_index = 0; break;
case 'default': break;
}
if ($case_index < 0) {
$case_index = 0;
$msg = 'You have reached the first case in the result set.';
$app->enqueueMessage($msg, 'warning');
}
if ($case_index >= count($cases_arr)) {
$case_index = count($cases_arr)-1;
$msg = 'You have reached the last case in the result set.';
$app->enqueueMessage($msg, 'warning');
}
if ( !empty($cases_arr[$case_index]) ) {
$app->setUserState('case_index', $case_index);
$app->setUserState('current_case', $cases_arr[$case_index]);
}
header("Content-Type: text/html; charset=utf-8");
print $app->getUserState('current_case');
$app->close();
}
}
不確定你是理想的做法。 請跟隨我們簡單的指南這里使用的Joomla Ajax組件。
在任何情況下,你的“考試”視圖應該在回顯數據后立即有一個die(),以免在其他地方轉移控制(如果你需要更多解釋,請告訴我)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.