weixin_33686714 2017-03-24 22:25 采纳率: 0%
浏览 4

Joomla和AJAX

I'm having a terrible time trying to get the ajax call below working in my Joomla 3.6 component. The javascript function below works in the sense that it does return the data and display it in the page--for a split second--then the page reloads, and I get the result of view.html.php, which is not what I need here. I get the same results whether I set the async property to true or false. It just happens much quicker with async set to true. The data returned in view.raw.php is fine. I must avoid reloading view.html.php. The javascript function getCase() is called by "onclick" of a button and I always clear the cache before testing. Thanks.

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

Entire display() function from view.raw.php:

    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();
    }
}
  • 写回答

1条回答 默认 最新

  • Lotus@ 2017-03-27 21:41
    关注

    Not sure you're doing it the ideal way. Please follow our simple guide here for using the Joomla Ajax component.

    In any case, your "examcase" view should have a die() immediately after echoing the data so as not to transfer control elsewhere (please let me know if you need more explanation on this).

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog