Ajax とサーバー コードを Joomla コンポーネントのどこに配置したかわかりません。Joomlaパート2のドキュメントに従って、単純なhello worldコンポーネントを作成しました(単純なコンポーネントだけで他のものは必要ありません)。
今、単純なjquery/ajax チュートリアルを使用して、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 を使用してコンポーネントを作成することの全体的なポイントは、joomla フレームワークを で使用できるようにすることですscript.php
。たとえば、次のような呼び出しを行います。$user =& JFactory::getUser();
前もって感謝します。