2013-04-05 59 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); 
     } 

     }); 
    }); 
");