0

我一直在寻找几个小时来解决这个问题。我有一个带有控制器的标准组件,因此:

defined('_JEXEC') or die;

jimport('joomla.application.component.controller');
jimport( 'joomla.environment.request' );

class GetajaxController extends JController
{
  function test()   
   {
       $jinput = new JInput();
       $myvar = $jinput->getVar('eventname');
       print_r($_REQUEST);
       $a = $_GET['eventname'];
       $event = JRequest::getVar( 'eventname' ) ;
       $client = JRequest::getVar( 'client' ) ;   
       echo "CLIENT:".$client." EVENT:".$event."*".$myvar;
   }
}

(我一直在尝试多种解决方案,这就是为什么那里有额外的废话但相关代码仍然存在)

因此,我从自定义模块中调用它:

$urlA = "index.php?option=com_getajax&task=test&format=raw";
$document = JFactory::getDocument();
$document->addScriptDeclaration("

    function runButton() {

       var data = 'eventname=aName&client=aClient';
       var url='$urlA';
       var request = new Request({
       url: url,
       data: data,
       method:'post',
       onSuccess: function(responseText){
         document.getElementById('xml_result').innerHTML=responseText;
       }
       }).post('eventname=foo&client=baR'); // ORIGINALLY HAD IT AS JUST .post()
       request.setHeader('eventname', 'sdfsdfsdf'); // ADDED
   }
");

返回的响应仅包含硬编码的“CLIENT: ... EVENT”减去变量。换句话说,我确实得到了响应,并且整个事情的 ajax/jquery 部分工作正常,只是我似乎无法成功地将参数发送到组件。(或者至少,在组件中检索它们)

我已经触发了它,但它们不在响应中。我什至对 url 进行了硬编码,并在控制器中使用了一个简单的 $_GET ,但没有成功;

$urlA = "index.php?option=com_getajax&task=test&format=raw&event=foo&client=bar";

我已经尝试过使用和不使用 sef url。您可以从控制器中看到我尝试了各种方法来捕获传递的参数。我也试过'get'和'post'。

顺便说一句,我已经尝试了所有通常发布的解决方案,所以我认为这与 joomla 以某种只有开发人员母亲才能欣赏的晦涩方式删除 url 参数有关。

  • 在萤火虫中,出现了一个“x”参数,我没有发送它似乎是空的。不知道这是否重要。

任何帮助都会很棒。

在此先感谢,杰夫

4

1 回答 1

0

如果有人觉得这很有用,我基本上需要将其解析为基础知识以使其正常工作。

虽然这涉及到很多试验和错误,但我还没有声称完全理解所有这些。

控制器:

define( '_JEXEC', 1 );

// need to get various libraries first for joomla specific controls

jimport( 'joomla.application.component.controller' );
jimport( 'joomla.environment.request' );

// we extend JController 

class GetajaxController extends JController {

  // the 'test' function will be a 'task' we can call in the url by the name 'test' 
  // (we can have many by simple defining new functions within our JController class)

    function test() {

        $app =& JFactory::getApplication();

        // get vars posted from module

        $foo = JRequest::getVar('aVar');
        $bar = JRequest::getVar('bVar');
        $result = doSomethingWithThem($foo,$bar);

        // encode result and send back. 
        // (using json encoding allows us multiple return variables. not used here.) 

        if($result) echo json_encode(array('test' => $result));

        // need to flush and close to prevent anything else but our data going back

        $app->close();
        return;
    }
}

// helper functions can be in the controller but not in the JController class

function doSomethingWithThem($foo,$bar) {
   return $foo . $bar;
}

模块:

define('_JEXEC', 1);

// bring in mootools (which I'm not convinced is needed here, but it works so...)

JHTML::_( 'behavior.mootools' );
JHtml::_( 'behavior.framework', true );

// add jscript to rendered page

$document->addScriptDeclaration("

   // add a function to an element event (a button in this case)
   jQuery('#btgetajax').live('click', function() {

      var aVar = "Something";
      var bVar = "Something else";

      // actually call the ajax component. notice the task=test. 
      // format=raw further asks the component to only return our data

      var url='index.php?option=com_getajax&task=test&format=raw';

      // json encode the data we're sending to the controller

      var dat = {'aVar':aVar, 'bVar':bVar};

      // send it via ajax

      jQuery.ajax({

         url: url,
         method: 'post',
         data: dat,
         dataType:'json',
         success: function(data) {
            alert(data.test);
         },
         error:function(xhr, status, data) {
            alert(status);
         }

      });
   });
");
于 2013-05-07T21:14:56.897 回答