如何正确编码 Joomla MVC 组件以执行 jQuery AJAX GETS/POSTS? [英] How to correctly code a Joomla MVC component to perform jQuery AJAX GETS/POSTS?

查看:21
本文介绍了如何正确编码 Joomla MVC 组件以执行 jQuery AJAX GETS/POSTS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有这个壮观的 Joomla 组件,完成后将带来世界和平,解决世界饥饿问题.我所要做的就是合并通过 jquery .ajax 调用调用的代码.我目前将它们放在外部位置,以便 AJAX 调用可以到达它们,但不清楚如何将它们正确放置在 Joomla 组件结构中.

So, I've got this spectacular Joomla component that when finished, will bring world peace, and solve world hunger. All I have to do is incorporate the code that gets called via the jquery .ajax calls. I currently have them in an external location so that the AJAX calls can get to them, but am not clear how to correctly place them in the Joomla Component structure.

我已经完成了研究,相信我,但只发现了很多关于你可以试试这个,或者那个"的讨论.大概是因为 Joomla 依赖于 mootools,所以 Jquery 通常不在讨论范围内,但我们内部有这个很棒的 jQuery 例程库,我想以正确的方式利用它.

I've done the research, trust me, but have only found a lot of discussion about "you could try this, or that". Presumably it's because Joomla relies on mootools, so Jquery is generally out of the discussions, but we have this great library of jQuery routines in-house that I'd like to leverage the right way.

在我看来,每次调用都必须创建一个视图,这似乎需要很多不必要的工作/开销来完成它.(就像要求飞行员与乘客一起办理登机手续).在我看来,它不应该这么难,所以我对我缺少的东西感到困惑,或者为什么似乎没有直接的答案.

It seems to me that a view has to be created for each call, which seems like a lot of unnecessary work/overhead to accomplish it. (Like asking the pilot to check in to his flight with the passengers). It also seems to me that it shouldn't be this difficult, so am confused about what I'm missing, or why there doesn't appear to be a straight answer.

更新解决方案接受了 Soren 的回答,因为它直接回答了问题,虽然我没有指定 Joomla 1.5,但我能够快速调整他的回答以适应工作.

UPDATE WITH SOLUTION Accepted answer from Soren because it answers the question directly, though I hadn't specified Joomla 1.5, I was able to quickly adapt his answer to work.

不过,值得一提的是 Greg P - 他的回答开辟了一个世界的其他可能性,我没有考虑过快速解决我所面临的其他项目/挑战.

Honorable mention to Greg P though - his answer opened up a world of other possibilities that i hadn't considered for quick solutions to other projects/challenges that I have.

这是我能够使用 AJAX 成功调用我的组件我的组件的 URL,以及我添加的内容以使其工作.

Here's the url I was able to successfully call from my component to my component using AJAX, and what I added to make it work.

index.php?option=com_mycomponent&format=raw&controller=ajax.raw&task=myfunction

componentscom_mycomponentcontrollersajaxraw.php

<?php
    defined('_JEXEC') or die( 'Restricted access' );
    jimport('joomla.application.component.controller');
    class MycomponentControllerAjaxraw extends JController
    {
        function myfunction()
        {
            echo json_encode("SomethingOrOther");
        }
    }
?>

推荐答案

您应该对

index.php?option=com_yourcomponent&task=ajax.function_name&format=raw

然后,您需要创建一个名为 ajax.raw.php 的新控制器,并在其中编写名称与 URL 中的function_name"匹配的函数,它们将在调用 URL 时执行.

You then need to make a new controller called ajax.raw.php and inside that you write functions with names that matches the "function_name" in the URL and they will be executed upon calling the URL.

在这些中你可以使用模型 $this->getModel();和视图等在需要时,甚至可以偷懒,将您的逻辑直接放在控制器中.

From within those you can use models $this->getModel(); and views etc. when needed, or even be lazy and put your logic directly in the controller.

如果您没有组件,请尝试使用 Joomla Component Creator

If you do not have a component then give the Joomla Component Creator a try

这篇关于如何正确编码 Joomla MVC 组件以执行 jQuery AJAX GETS/POSTS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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