Dear Coders!
我的代碼的目的:在特定的文件夾中列出的文件
獲取URL,然後將其分配給在JavaScript中的數組。
我如何想象它: 在test.php
中的JavaScript函數使用$.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:
函數(數據,狀態)`。
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;
}
?>
注:測試我使用谷歌瀏覽器。
所以我就這麼想,希望有人能給我一個解決方案和可能的解釋。
可能重複://計算器。 com/questions/14220321/how-to-return-the-an-ajax-call) –