2013-04-05 60 views
0

我一直在寻找小时解决这个问题。我有一个控制器,因此标准组件:Joomla 2.5 mootools ajax - 发送参数到组件

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

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

我已经firebugged它,他们没有在响应。我甚至硬编码的网址,并在控制器中使用一个简单的$ _GET没有成功;

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

我试过了,没有sef urls。你可以从控制器看到我已经尝试了各种方法来捕获传递的参数。我也尝试了'get'和'post'。

我试过所有通常发布的解决方案顺便说一句,所以我认为这与joomla以一些晦涩的方式让网址参数出来,只有开发人员的母亲可以欣赏。

  • 在firebug中,有一个'x'参数显示我没有发送,看起来是空的。不知道这是否重要。

任何帮助将是伟大的。

由于提前,杰夫

回答

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

     }); 
    }); 
");