2012-07-29 120 views
0

我很難弄清楚我的Ajax和服務器代碼放在Joomla組件中的位置。我在Joomla part 2 docs之後創建了一個簡單的Hello World組件(我不需要任何其他的東西只是一個簡單的組件)。在Joomla 2.5組件中使用jQuery與Ajax

現在我試圖使用簡單的jquery/ajax tutorial jQuery添加Ajax代碼。所以我添加此代碼:

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("script.php"); 
    }); 
    }); 
    </script> 
<style type="text/css"> 
    #wrapper { 
     width: 240px; 
     height: 80px; 
     margin: auto; 
     padding: 10px; 
     margin-top: 10px; 
     border: 1px solid black; 
     text-align: center; 
    } 
    </style> 
</head> 
<body> 
    <div id="wrapper"> 
    <div id="quote"><p> </p></div> 
    <input type="submit" id="generate" value="Generate!"> 
    </div> 
</body> 
</html> 

在我增加了對服務器端處理的script.php的文件在同一目錄。同樣,剛剛從教程:

<?php 
header("Cache-Control: no-cache"); 
// Ideally, you'd put these in a text file or a database.  
// Put an entry on each line of 'a.txt' and use $prefixes = file("a.txt"); 
// You can do the same with a separate file for $suffixes. 
$prefixes = array('Mashup','2.0','Tagging','Folksonomy'); 
$suffixes = array('Web','Push','Media','GUI'); 
// This selects a random element of each array on the fly 
echo $prefixes[rand(0,count($prefixes)-1)] . " is the new "  
    . $suffixes[rand(0,count($prefixes)-1)]; 
// Example output: Tagging is the new Media 
?> 

我猜我指定的script.php的方式是不對的,因爲我得到的生成按鈕,當我訪問該組件:

http://mysite.com/index.php?option=com_mycomponent

編輯:沒有注意到這是非常關鍵的錯誤。我收到一個Not Found錯誤:http://mysite.com/script.php。這顯然不在那裏。我在哪裏爲我的組件?請記住,使用ajax創建組件的重點在於,我可以在script.php中使用joomla框架。例如,進行如下呼叫:$user =& JFactory::getUser();

在此先感謝。

回答

0

嘗試使用

組件/ com_mycomponent /視圖/ myComponent的/負載功能TMPL/script.php的

0

而不是使用.Load()方法,請嘗試在您的點擊處理程序中使用它。

$.ajax({ 
    url: 'script.php', 
    success: function(data) { 
    $('#quote p').html(data); 
    } 
}); 
2

1把script.php的內容在公共功能的主要成分(重要的)控制器例如ajaxit()。

2-修改您單擊處理程序如下:

$.ajax({ 
    url: 'index.php?option=com_mycomponent&view=mycomponent&task=ajaxit&format=raw', 
    success: function(data) { 
     $('#quote p').html(data); 
    } 

});