Reputation: 2634
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?
Upvotes: 2
Views: 4363
Reputation: 21
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>
Upvotes: 2
Reputation: 2899
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
Upvotes: 4