我一直在寻找几个小时来解决这个问题。我有一个带控制器的标准组件:
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网址。你可以从控制器上看到我已经尝试了各种方法来捕获传递的参数。我也尝试了'get'和'post'。
我已经尝试了所有常见的解决方案,所以我认为这与joomla以某种模糊的方式调整url参数有关,只有开发人员母亲才能欣赏。
任何帮助都会很棒。
提前致谢, 杰夫
答案 0 :(得分: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);
}
});
});
");