没有正式的方法可以从模块中执行 ajax。
如果你在哪里开发一个组件,你可以调用你的组件
index.php?option=com_yourcomponent&task=getjsondata&view=yourview&format=json
这样你的文件views/yourview/view.json.php
就会被调用,你可以在那里把你的数据发送出去,echo json_encode( array("success"=>true) ); jexit()
请注意使用jexit()
insted oexit()
如果你真的需要从一个模块做 ajax,这里有一个在你的文件中导入 joomla 的技巧
<?php
if (! class_exists('JFactory') ) {
define( 'DS', DIRECTORY_SEPARATOR );
$rootFolder = explode(DS,dirname(__FILE__));
$currentfolderlevel = 2;
array_splice($rootFolder,-$currentfolderlevel);
$base_folder = implode(DS,$rootFolder);
if(is_dir($base_folder.DS.'libraries'.DS.'joomla'))
{
define( '_JEXEC', 1 );
define('JPATH_BASE',implode(DS,$rootFolder));
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
// Joomla is loaded! horray!
$email = JRequest::getVar('email');
$tags = implode(",",JRequest::getVar('tags',array()));
$db = JFactory::getDbo();
$db->setQuery("INSERT INTO `#__newsletter_users` (email,tags) VALUES('$email','$tags')");
$db->query();
}
}
?>
save_email.php
在您的模块文件夹中创建一个像上面的文件(对我来说)并直接调用它。这是我的模块模板的一部分:
$.get('<?php echo JUri::base()."modules/mod_newsletter/save_email.php?email=" ?>'+email, function(data) {
console.log(data);
modal.hide();
});