Jquery Joomla 2.5 mootools ajax-向组件发送参数
我已经寻找了几个小时来解决这个问题。我有一个带控制器的标准组件,因此:Jquery Joomla 2.5 mootools ajax-向组件发送参数,jquery,ajax,joomla,Jquery,Ajax,Joomla,我已经寻找了几个小时来解决这个问题。我有一个带控制器的标准组件,因此: defined('_JEXEC') or die; jimport('joomla.application.component.controller'); jimport( 'joomla.environment.request' ); class GetajaxController extends JController { function test() { $jinput = new
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部分工作得很好,只是我似乎一辈子都无法成功地将参数发送到组件。(或至少在组件中检索它们)
我已经安装了Firebug,但他们不在响应中。我甚至对url进行了硬编码,并在控制器中使用了一个简单的$\u GET,但没有成功
$urlA = "index.php?option=com_getajax&task=test&format=raw&event=foo&client=bar";
我已经尝试过它与sef网址和没有。您可以从控制器中看到,我尝试了各种方法来捕获传递的参数。我也试过“get”和“post”
顺便说一句,我已经尝试了所有常用的解决方案,所以我认为这与joomla以一种只有开发者母亲才能理解的模糊方式修改url参数有关
- 在firebug中,有一个“x”参数显示为空,我没有发送它。不知道这是否重要
Jeff如果有人觉得这很有用,我基本上需要把它分解成基本的东西,让它工作起来 不过,这涉及到很多尝试和错误,我还没有完全理解 控制器:
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);
}
});
});
");