简体   繁体   English

Joomla和AJAX

[英]Joomla and AJAX

I'm having a terrible time trying to get the ajax call below working in my Joomla 3.6 component. 我正在努力让我的Joomla 3.6组件中的ajax调用正常工作。 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. 下面的javascript函数工作的意思是它确实返回数据并在页面中显示它 - 一瞬间 - 然后页面重新加载,我得到view.html.php的结果,这不是我的需要在这里。 I get the same results whether I set the async property to true or false. 无论我将async属性设置为true还是false,我都得到相同的结果。 It just happens much quicker with async set to true. 异步设置为true时,它发生得更快。 The data returned in view.raw.php is fine. view.raw.php中返回的数据很好。 I must avoid reloading view.html.php. 我必须避免重新加载view.html.php。 The javascript function getCase() is called by "onclick" of a button and I always clear the cache before testing. javascript函数getCase()由按钮的“onclick”调用,我总是在测试之前清除缓存。 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: 来自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();
    }
}

Not sure you're doing it the ideal way. 不确定你是理想的做法。 Please follow our simple guide here for using the Joomla Ajax component. 请跟随我们简单的指南这里使用的Joomla Ajax组件。

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). 在任何情况下,你的“考试”视图应该在回显数据后立即有一个die(),以免在其他地方转移控制(如果你需要更多解释,请告诉我)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM