简体   繁体   中英

Class 'JFactory' not found

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.

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

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

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

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

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

(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.

I hope it helped

I use this to solve the problem. I get the variable while I'm in the joomla framework. Then I pass the User_Name variable in my ajax call...

Hope this helps

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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