简体   繁体   English

找不到类“ JFactory”

[英]Class 'JFactory' not found

I've been creating some modules in Joomla using Jumi. 我一直在使用Jumi在Joomla中创建一些模块。 So I can write any php/javascript code and create a Jumi module that I can display where I want. 因此,我可以编写任何php / javascript代码并创建一个Jumi模块,该模块可以显示在所需的位置。

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: 我这样做已经有一段时间了,但是现在我正在尝试使用Jquery进行一些AJAX开发,但是出现了这个错误:

Class 'JFactory' not found in api.php

So I have a PHP file with the jQuery code: 所以我有一个带有jQuery代码的PHP文件:

$(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
        }
    });
});

As you can see it calls the api.php script to do some server processing. 如您所见,它调用api.php脚本进行一些服务器处理。 This file has a number of joomla calls like: 该文件有许多joomla调用,例如:

$user = &JFactory::getUser();

So why in this case do I not have the Joomla framework available? 那么为什么在这种情况下我没有Joomla框架呢?

The problem is that your Ajax call ends up in a file out of the Joomla "platform". 问题是您的Ajax调用最终会出现在Joomla“平台”之外的文件中。 The proper way to do that, if possible, is to make the ajax call something like: 如果可能的话,正确的方法是使ajax调用类似以下内容:

index.php?option=yourcomponent&controller=xxx&task=yyy 的index.php?选择= yourcomponent&控制器= XXX&任务= YYY

(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. (这意味着您应该在该组件内部有一个组件“ mycomponent”和一个控制器“ xxx”),然后该控制器应负责处理ajax调用并发送响应。 You can return json-encoded response for example or anything you need. 您可以返回json编码的响应,例如您需要的任何内容。

I hope it helped 希望对您有所帮助

I use this to solve the problem. 我用它来解决问题。 I get the variable while I'm in the joomla framework. 我在joomla框架中时得到了变量。 Then I pass the User_Name variable in my ajax call... 然后我在ajax调用中传递User_Name变量...

Hope this helps 希望这可以帮助

<script type="text/javascript">
  var User_Name = '<?php $user =& JFactory::getUser(); $User_Name = $user->username; echo $User_Name; ?>';
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM