Joomla 3.x在Ajax Call

时间:2015-05-04 14:05:33

标签: php ajax joomla

我正在使用Joomla 3.x中的自定义构建模块,它使用Ajax来显示一些实时数据。在Ajax调用执行的函数中,我需要访问模块参数,我不知道如何执行此操作。通常情况下,我可以在模块PHP文件中放入$ var = $ params(' paramName','默认')来获取参数,但是当调用时,这不可用阿贾克斯。这是执行Ajax调用的模板代码:

<script type="text/javascript">
  jQuery(document).ready(function() {
    jQuery.get('index.php?option=com_ajax&module=whatsinport&method=getWhatsInPort&format=json', function(data) {
      console.log(data);
      var response = jQuery.parseJSON(data);

这是我的helper.php类中的代码:

class modWhatsInPortHelper {

  public static function getWhatsInPortAjax() 
  {
    $results = array();
    $results['status'] = 'ok';

    $app = JFactory::getApplication();
    $serverName = $app->getCfg('mod_whatsinport_serverName');
    $dbName = $app->getCfg('mod_whatsinport_dbName');
    $dbUser = $app->getCfg('mod_whatsinport_dbUser');
    $dbPwd = $app->getCfg('mod_whatsinport_dbPwd');

    $connectionInfo = array( "Database"=>$dbName, "UID"=>$dbUser, "PWD"=>$dbPwd, "ReturnDatesAsStrings"=>true);
    $conn = sqlsrv_connect( $serverName, $connectionInfo);

$ app-&gt; getCfg似乎没有做我想要的 - 我猜它只用于Joomla配置设置。我也尝试过:

  $app = JFactory::getApplication();
  $params = $app->getParams();
  $serverName = $params->get('mod_whatsinport_serverName');
  $dbName = $params->get('mod_whatsinport_dbName');
  $dbUser = $params->get('mod_whatsinport_dbUser');
  $dbPwd = $params->get('mod_whatsinport_dbPwd');

但这也没有奏效。我忘了包含我的模块配置文件:

<?xml version="1.0" encoding="utf-8"?>
<extension type="module" version="3.1.0" client="site" method="upgrade">
    <name>WhatsInPort</name>
    <author>Chris Krohn</author>
    <version>1.0.0</version>
    <description>Displays a list of current vessels in port.</description>
    <files>
        <filename>mod_whatsinport.xml</filename>
        <filename module="mod_whatsinport">mod_whatsinport.php</filename>
        <filename>index.html</filename>
        <filename>helper.php</filename>
        <filename>tmpl/default.php</filename>
        <filename>tmpl/index.html</filename>
    </files>
    <config>
      <fields name="params">
        <fieldset name="basic">
          <field name="mod_whatsinport_serverName" type="text" default="" label="Server Name" description="NETBIOS name of the database server to connect to." />
          <field name="mod_whatsinport_dbName" type="text" default="" label="Database Name" description="Name of the database with the HMLOG table." />
          <field name="mod_whatsinport_dbUser" type="text" default="" label="Database User" description="User name to login to the server with." />
          <field name="mod_whatsinport_dbPwd" type="password" default="" label="Database Password" description="Password to login to the server with." />
        </fieldset>
      </fields>
    </config>
</extension>

2 个答案:

答案 0 :(得分:1)

想出来。应该是:

  $app = JFactory::getApplication();
  $module = JModuleHelper::getModule('mod_whatsinport','WhatsInPort');
  $params = new JRegistry($module->params);       
  $serverName = $params->get('mod_whatsinport_serverName');
  $dbName = $params->get('mod_whatsinport_dbName');
  $dbUser = $params->get('mod_whatsinport_dbUser');
  $dbPwd = $params->get('mod_whatsinport_dbPwd');

答案 1 :(得分:0)

"Onerous"方法中检索参数的数据。

getjax()