コーダーの皆さん!
私のコードの目的:
特定のフォルダーにリストされているファイルの URL を取得し、それらを JavaScript の配列に割り当てます。
私が想像している方法: の JavaScript 関数は、メソッドをtest.php
使用$.post()
して値をgetURL.php
ファイルに送信します。その後、getURL.php
この値を使用して、特定のフォルダー内の特定のファイル URL を取得します。$.post()
メソッドfunction(data)
パラメータで結果を取得しています。この後、「データ」の結果の値が JavaScript で使用されます。
問題:$.post()
メソッド関数内: function(data,status) I'm satisfied with the result of the returned value of the data parameter; the PROBLEM is that I can't assign it's value outside this function:
function (data,status)`.
TEST.php
<script src="jquery-1.9.1.js">
</script>
<script type="text/javascript">
var imgPath; // <--He is who should get the value of "data"
function getTargetUrl(szolg){
$.post(
"getURL.php",
{ x: szolg },
function(data,status){
alert("in function: " + data + " status: " + status);
imgPath=data;
alert (imgPath);
}
);
}
$(document).ready(function() {
var a="szolg5"; //it will be "user defined", used in getURL.php
getTargetUrl(a);
alert(imgPath);
});
</script>
getURL.php
<?php
if(isset($_POST["x"])){
$queryGlob='img/'.$_POST["x"].'/batch/*.jpg';
foreach (glob($queryGlob) as $filename) {
$imgFiles=json_encode($filename);
$imgFiles=str_replace('\/','/',$imgFiles);
echo $imgFiles;
}
//$data = str_replace('\\/', '/', json_encode(glob('img/'.$_POST["x"].'/batch/*.jpg')));
}
else{
$imgFiles="FAIL";
echo $imgFiles;
}
?>
注: テストのために、Google Chrome を使用しています。
誰かが私に解決策と可能な説明を教えてくれることを願っています。