Joomla with Ajax:致命错误:找不到“JFactory"类 [英] Joomla with Ajax: Fatal error: Class 'JFactory' not found

查看:25
本文介绍了Joomla with Ajax:致命错误:找不到“JFactory"类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在弄清楚如何一起使用 Ajax 和 Joomla 框架时仍然存在问题.我创建了一个 Joomla 组件,我可以使用它来访问:

index.php?option=com_mycomponent

我在 components/com_mycomponent/views/mycomponent/tmpl/default.php 中使用 Jquery 和 Ajax:

<头><title>Ajax 和 jQuery 示例</title><script language="javascript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script><脚本类型=文本/JavaScript">$(document).ready(function(){$("#generate").click(function(){$("#quote p").load("components/com_mycomponent/views/mycomponent/tmpl/script.php");});});<身体><div id="包装器"><div id="quote"><p></p></div><input type="submit" id="generate" value="Generate!">

然后在 script.php 文件中我有这个:

如果我没有在 script.php 中放置任何 Joomla 框架代码,它就可以正常工作.但这样做的目的是我需要使用 Joomla 框架,因此是创建组件的全部意义.但我仍然不明白我需要如何构建 Joomla 组件,所以我没有收到 Class 'JFactory' not found 错误?

解决方案

最后并没有花太长时间.唯一需要的文件是default.php,因此您可以删除其中的script.php 和其他奇怪的文件.

Default.php

addScript("https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js");$user =&JFactory::getUser();?><div id="包装器"><div id="quote" style="display:none; padding-bottom:10px;"><?phpecho "<p>这是您的用户名:".$user->用户名."</p>";echo "<p>这是你的真名:".$user->name ."</p>";echo "<p>这是您的用户 ID:".$user->id ."</p>";?>

<input type="submit" id="generate" value="Generate!">

<script type="text/javascript">$(document).ready(function(){$("#generate").click(function(){$("#quote").show();});});

我已经注释掉了代码中的 jquery 引用,因为 widgetkit 已经加载了它的一个副本,但将它保留在那里以防万一.

这不是加载另一个文件,而是隐藏 #quote div 标签,当单击按钮时,它会显示其中的数据.在这个中,我添加了 realnameuser ID 以防万一.

希望这会有所帮助.

问候

Still having problems figuring out how to use Ajax and the Joomla framework together. I've created a Joomla component which I can access with:

index.php?option=com_mycomponent

I'm using Jquery with Ajax in components/com_mycomponent/views/mycomponent/tmpl/default.php:

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
?>
<html>
<head>
  <title>Ajax with jQuery Example</title>

<script language="javascript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/JavaScript">
  $(document).ready(function(){
    $("#generate").click(function(){
  $("#quote p").load("components/com_mycomponent/views/mycomponent/tmpl/script.php");

    });
  });
  </script>
</head>
<body>
  <div id="wrapper">
    <div id="quote"><p> </p></div>
    <input type="submit" id="generate" value="Generate!">
  </div>
</body>
</html>

Then in the script.php file I have this:

<?php  
$user =& JFactory::getUser();
echo "This is the user: ".$user;
?> 

If I don't put any Joomla framework code in script.php it works fine. But the purpose of doing this is that I need to use the Joomla framework and hence the whole point of creating a component. But I still don't understand how I need to structure the Joomla component so I don't get the Class 'JFactory' not found error?

解决方案

Didn't end up taking too long in the end. The only file that is required is the default.php, so you can delete the script.php and other odd files you have in there.

Default.php

<?php
defined('_JEXEC') or die('Restricted access');

//$document =& JFactory::getDocument();
//$document->addScript("https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js");
$user =& JFactory::getUser();
?>

<div id="wrapper">
    <div id="quote" style="display:none; padding-bottom:10px;">
        <?php 
          echo "<p>This is your username: " . $user->username . "</p>"; 
          echo "<p>This is your realname: " . $user->name . "</p>"; 
          echo "<p>This is your user ID: " . $user->id . "</p>";
        ?>
    </div>
    <input type="submit" id="generate" value="Generate!">
</div>

<script type="text/javascript">
$(document).ready(function(){
     $("#generate").click(function(){
          $("#quote").show();
     });
});
</script>

I have commented out the jquery reference in the code, seeing as the widgetkit is already loading a copy of it, but kept it there just incase.

Instead of loading another file, what this does is, it hides #quote div tag and when the button is clicked, it reveals it with the data inside it. In this one, I have added realname and user ID just incase.

Hope this helps.

Regards

这篇关于Joomla with Ajax:致命错误:找不到“JFactory"类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆