使用 Joomla,我如何进入 AJAX 调用,仅调用所调用的 php 函数生成的输出?
我正在创建一个 Joomla 组件。 在前端的视图中,我有一个带有 2 个下拉菜单的表单,通过从第一个选项中选择一个选项,第二个选项必须相应更改。 为此,我使用 AJAX。
我可以将请求发送到位于controller.php 文件内的php 函数,并且php 函数会生成正确的输出。
问题是 Joomla 框架将此输出放入包含所有元标记、标头、模板等的页面中,因为在将其返回给 AJAX 之前它应该显示在浏览器上。 您可以看到我收到的 Firebug 的屏幕截图。 我想放置下拉菜单的div是-div id="select-formatocarta"-,你可以看到在“select”之前我还有很多其他不需要的东西。 http://img695.imageshack.us/img695/7725/selectp.jpg
结果是,在我只想放置由 php 函数生成的“选择”菜单的 div 中,我得到了整个页面。即使我可以显示从 AJAX 请求传递 tmpl=component 参数的唯一菜单,div 高度就像整个页面都在其中一样。
如何在没有 Joomla 模板的情况下接收唯一的 php 函数的输出? 如果这是不可能的,我怎样才能从responseText中提取唯一的下拉菜单? 我只能使用 javascript,我没有可用的原型或 jquery。
I'm creating a Joomla component.
In a frontend's view I have a form with 2 drop down menus by choosing an option from the 1st, the options of the 2nd must change accordingly.
To do so I'm using AJAX.
I can send the request to the php function placed inside the controller.php file and the php function generates the correct output.
The problem is that Joomla framework put this output inside a page with all the meta tags, header, the template and so on as it should be shown on the browser before to give it back to AJAX.
You can see a screenshot from firebug of what I receive.
The div where I want to put the drop down menu is -div id="select-formatocarta"-, you can see that before the "select" I have many other unwanted things.
http://img695.imageshack.us/img695/7725/selectp.jpg
The result is that inside the div where I want to only put the "select" menu generated by the php function, I get whole page. Even if I can show the only menu passing the tmpl=component parameter from the AJAX request, the div height is like the whole page is inside it.
How can I receive the only php function's output without the Joomla template?
If that's not possible, how can I extract the only drop down menu from the responseText?
I can only use javascript, I don't have prototype or jquery available.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
出口。
因此,Joomla 将首先评估您的组件代码,然后通过模板系统添加页眉/页脚(这就是为什么组件可以动态添加 CSS 等标题行)。因此,您需要做的就是立即退出,阻止 Joomla 添加页眉/页脚。
因此,在您的控制器代码中,您将拥有类似以下内容:
内置的 PHP
exit()
函数也可以正常工作,但jexit()
是首选。Exit.
So Joomla will evaluate your component code first, then add the header/footer via the templates system later (this is why components dynamically can add header lines for CSS etc). So all you need to do is exit immediately, stopping Joomla from being able to add the header/footer.
So in your controller code, you'd have something like:
The built in PHP
exit()
function would also work fine, butjexit()
is preferred.您需要在 /components/com_yourcomponent/controllers/ 文件夹中创建一个名为 ajax.raw.php 的子控制器,
然后您可以像这样向它发送 ajax 请求:
index.php?option=com_name&task=ajax.function_name&format= raw&var=value
You need to create a sub controller in your /components/com_yourcomponent/controllers/ folder called ajax.raw.php
You can then send ajax requests to it like this:
index.php?option=com_name&task=ajax.function_name&format=raw&var=value
您不必为此创建新的控制器。您只需使用现有控制器并将 &format=raw 附加到 URL 即可。这会关闭 Joomla 的所有输出。为 ajax 调用创建一个特殊的控制器可能是一个很好的做法,以防您的组件很大并且难以维护。
使用 jexit();单纯的压制输出是绝对不好的。
You don't have to create a new controller for this. You can simply use your existing controller and append &format=raw to the URL. That switches off all output by Joomla. Creating a special controller for the ajax call is just something that could be good practice in case your component is big and difficult to maintain otherwise.
Using jexit(); simply to supress the output is absolutely bad.