This postは、サンプルとして、サーバーファイルリストを取得してサンプルコードを提供しています。私の問題はalert('index ' + key + ' points to file ' + val);
警報Javascriptをページに何も表示されないということであるJavascriptはPHP関数を呼び出して結果を返します
$dir = $_REQUEST['dir'] ;
$dir = $_SERVER['DOCUMENT_ROOT'] . $dir;
$filesArray = array();
$Counter = 0;
$files = scandir($dir);
foreach ($files as &$file) {
if ($file!='.' && $file!='..') {
$filesArray[$Counter] = $file;
echo $filesArray[$Counter].'';
$Counter++;
}
}
echo json_encode($filesArray);
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>How to create form using jQuery UI</title>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/pepper-grinder/jquery-ui.css" media="screen" rel="stylesheet" type="text/css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$.get('getfilename.php', { dir : '/ajax1/' }, function(data) {
// you should now have a json encoded PHP array
$.each(data, function(key, val) {
alert('index ' + key + ' points to file ' + val);
});
}, 'json');
alert ("ok");
});
</script>
</head>
<body>
<h1>Get file names... </h1>
</body>
</html>
getfilename.php:ここで私が使用しているコードです。私はFirebugのコンソールログで応答を得るため、スクリプトが動作しています。
ajax.jsajax.phpindex.html["ajax.js","ajax.php","index.html"]
この情報をhtmlページに戻すためにJSONを使用するために変更する必要があるものは何ですか?
ありがとうございました。
何あなたのjavascriptコンソールに 'console.log(data);'の出力?コンソールエラーはありますか? – Robbert
ありがとうRobbert - Firebugを使用しましたが、Consoleセクションにエラーは表示されません。 – PeterK900