将 AJAX 放入 Joomla 模块 [英] Putting AJAX in a Joomla Module

查看:24
本文介绍了将 AJAX 放入 Joomla 模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为我的网站制作了一个基本的 Joomla 模块作为一个聊天框.但我想将 AJAX 放入其中(我知道 JED 上已经存在一个与 AJAX 类似的模块,但这更像是一个让我了解 AJAX 在 Joomla 模块中工作方式的项目).

I've made a basic Joomla module for my site as a shoutbox. But I'd like to put AJAX in it (I know a similar module with AJAX already exists on JED but this more a project for me to learn how AJAX works in a Joomla module).

通常重定向到新 php 文件的 AJAX 内容显然不起作用,因为该文件不会被定义为

The usual AJAX stuff where you redirect to a new php file obviously doesn't work as the file will not be defined as

   defined('_JEXEC') or die('Restricted access');

将在新页面中失败.将 _JEXEC 定义为等于 1(正如我在关于 SO 的几篇文章中读到的)就我在 Joomla Docs 上读到的而言是一种安全风险,因为它提供了一个进入站点的入口点.

will fail in a new page. And defining _JEXEC to be equal to one (as I've read in several posts on SO) as far as I've read on Joomla Docs is a security risk as it provides an entry point onto the site.

我见过的其他shoutbox模块的做法是指出回到 helper.php 文件中的一个函数.这对我来说很有意义,因为这是通常应该存储所有功能的地方.但是,我不清楚模块如何通过 onSubmit()(或相关)命令访问 helper.php 文件,并希望有人能对此有所了解.

The way the other shoutbox module I've seen does is to point back at a function in the helper.php file. Which makes sense to me as that is where all the functions should normally be stored. However I'm unclear as to how to the module was accessing the helper.php file on a onSubmit() (or a related) command and was hoping someone could shed some light on this.

我实际上不需要任何特定于我的 Shoutbox 模块的东西 - 这更多是关于如何在 Joomla 模块 中获得 AJAX 功能以及它是如何安排的问题

I don't actually need anything specific to my shoutbox module - this is more a question of how to get AJAX functionality in Joomla modules and how it is arranged

推荐答案

另外两个是正确的,您需要记住,当您进行 AJAX 调用时,您无法直接访问 Joomla 中的 php 文件.因此,最好调用您的模块.在这种情况下,让模块检查 POST 或 URL 中的变量.

The other two were onto the right track you need to remember that when you make your AJAX call you can't directly access a php file in Joomla. So instead it's better to make calls to your module. In this case and have the module check for variables in your POST or in the URL.

我是 JQuery ajax 的忠实粉丝,它比构建该喊叫框的人使用的方法更加独立.

I'm a big fan of JQuery's ajax it's a lot more self contained than the method the guy who built that shoutbox used.

$( "#addShout" ).click( function(event, ui) {
                $.ajax({
                    type: 'GET',
                    url: "<?php echo JURI::base() . "index.php?option=mod_mymodule&task=getCustomerJson&selectedCustomer="?>"  + encodeURIComponent(value),
                    success:function(data){
                        $('#shouts').append(data);
                    },
                    error:function(){
                        $('#errors').html('<p class="error"><strong>Oops!</strong> Try that again in a few moments.</p>');
                    }
                });  
        });

然后正如我在对 Valentin 的评论中提到的:

Then as I mentioned in my comment to Valentin you:

$task = JRequest::getVar('task'); 
if($task == "getCustomerJson"){mySuperHelper::getCustomerJson();}

只有在变量存在时才调用必要的函数.

You only call the necessary functions when the variables exist.

为了解释这个过程的一部分,它有点像这样:

To explain part of the process it's kinda like this:

  1. 如果 POST 或 URL 中没有变量,它将简单地按照 Joomla 期望的方式显示模块.
  2. 如果检测到变量,则调用将其添加到数据库的方法和将其添加到显示中的 javascript 函数.也防止了正常显示的发生.

您引用的模块非常有趣.主文件引用的辅助函数完成了模型通常在 MVC 中处理的任务,而 Javascript 也有点结束了.如果您真的想了解它是如何工作的,您真的需要深入研究 fatAjax.js 文件,因为它包含所有 AJAX 并设置 mod_shoutbox.php 侦听的变量.

The module you referenced was quite interesting. The helper functions that the main file referenced does what a model would normally handle in MVC and the Javascript was also kinda all over. If you really want to understand how that one worked you really need to dig into the fatAjax.js file as it contains all of the AJAX and sets the variables that the mod_shoutbox.php listens for.

这篇关于将 AJAX 放入 Joomla 模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆