未找到“JFactory"类

2022-01-06 00:00:00 jquery php joomla

我一直在使用 Jumi 在 Joomla 中创建一些模块.所以我可以编写任何 php/javascript 代码并创建一个 Jumi 模块,我可以在我想要的地方显示.

I've been creating some modules in Joomla using Jumi. So I can write any php/javascript code and create a Jumi module that I can display where I want.

我已经这样做了一段时间没有问题,但现在我正在尝试使用 Jquery 进行一些 AJAX 开发,我收到此错误:

I've been doing this for a while without problems but now that I'm trying some AJAX development with Jquery I'm getting this error:

Class 'JFactory' not found in api.php

所以我有一个带有 jQ​​uery 代码的 PHP 文件:

So I have a PHP file with the jQuery code:

$(function() {
    $.ajax({
        url: 'ajax_dashboard/api.php',                  //the script to call to get data
        data: "",
        dataType: 'json',                //data format
        success: function(data)          //on recieve of reply
        {
            var id = data[0];              //get id
            var vname = data[1];           //get name

            $('#output').append("<b>id: </b>"+id+"<b> name: </b>"+vname)
                .append("<hr />"); //Set output element html
        }
    });
});

如您所见,它调用 api.php 脚本来进行一些服务器处理.该文件有许多 joomla 调用,例如:

As you can see it calls the api.php script to do some server processing. This file has a number of joomla calls like:

$user = &JFactory::getUser();

那么为什么在这种情况下我没有可用的 Joomla 框架?

So why in this case do I not have the Joomla framework available?

推荐答案

问题是您的 Ajax 调用最终出现在 Joomla平台"之外的文件中.如果可能,正确的方法是使 ajax 调用类似于:

The problem is that your Ajax call ends up in a file out of the Joomla "platform". The proper way to do that, if possible, is to make the ajax call something like:

index.php?option=yourcomponent&controller=xxx&task=yyy

index.php?option=yourcomponent&controller=xxx&task=yyy

(这意味着您应该在该组件内有一个组件mycomponent"和一个控制器xxx")然后控制器应该负责处理ajax调用并发送响应.例如,您可以返回 json 编码的响应或您需要的任何内容.

(it means you should have a component "mycomponent" and a controller "xxx" inside that component ) Then the controller should be responsible to handle the ajax call and send a response. You can return json-encoded response for example or anything you need.

希望能帮到你

相关文章