如何在Joomla模块中使用jQuery Ajax [英] How to use jQuery ajax in Joomla module

查看:143
本文介绍了如何在Joomla模块中使用jQuery Ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个模块,其中在我的主要PHP文件中添加了对Javascript文件的引用.在该Javascript文件中,我对dom ready进行了jQuery调用,该调用执行Ajax发布以获取div的初始内容.它发布到的URL是我模块的另一个PHP文件(gateway.php). (此gateway.php调用远程服务器以获取内容.)

I'm creating a module where in my main PHP file I've added a reference to a Javascript file. In that Javascript file, I do a jQuery call on dom ready that does an Ajax post to grab the initial content for a div. The URL that it posts to is another PHP file (gateway.php) that is part of my module. (This gateway.php does a call to a remote server to get the content.)

好吧,通常禁止直接访问模块文件.我可以注释掉禁止直接访问的代码行.即使这样做,由于某种原因,我的gateway.php似乎也无法完全访问Joomla中的所有内容.例如,gateway.php需要我的helper.php文件.我只是在做

Well, direct access is typically prohibited for module files. I can comment out the line of code that prohibits direct access. Even doing that, for some reason it seems like my gateway.php doesn't have full access to everything in Joomla. For example, gateway.php has a require of my helper.php file. I'm just doing

require_once(dirname(__FILE__) . DS . 'helper.php');

据我所知,

是包含文件的标准方法.但是,我收到错误消息,表明DS没有被转换为Joomla中定义的值.它把DS当作一个字符串值对待.

which as far as I can tell is the standard way to include a file. However, I get errors showing that DS isn't getting translated as the value defined within Joomla. Its treating DS like a string value.

如果我将其更改为

require_once(dirname(__FILE__) . '/helper.php');

然后我收到禁止直接访问的错误.

then I get the direct access prohibited error.

您知道这里发生了什么吗,还是在除主要PHP模块文件之外的其他文件中的Joomla模块中使用Ajax的正确方法?

Any idea what's going on here or the correct way to using Ajax in a Joomla module in a file other than the main PHP module file?

推荐答案

除非有绝对的必要,否则您应该始终通过Joomla的index.php文件进行调用.不要尝试调用单个PHP文件,除非它们100%独立于Joomla.调用Joomla中每个.php文件顶部的defined( '_JEXEC' ) or die( 'Restricted access' );的原因是:您希望最大程度地减少进入Joomla的入口点的数量.

Unless there's an specific reason you absolutely need to do otherwise, you should always make your calls through Joomla's index.php file. Don't attempt to call individual PHP files, unless they are 100% independent of Joomla. The calls to defined( '_JEXEC' ) or die( 'Restricted access' ); at the top of every .php file in Joomla for a reason: you want to minimize the number of entry points into Joomla.

要进行从Joomla中检索数据的AJAX/JSON调用,您需要创建一个与组件一起使用的小组件.几个月前,我写了一篇有关此的博客文章: http://www.designvsdevelop.com/the-way-not-to-do-javascript-in-joomla

To make AJAX/JSON calls that retrieve data from within Joomla, you'll want to create a small component to go along with your module. I wrote a blog post about this a few months back: http://www.designvsdevelop.com/the-way-not-to-do-javascript-in-joomla

这篇关于如何在Joomla模块中使用jQuery Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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