2013-03-30 65 views
0

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; 
} 
?> 

注:測試我使用谷歌瀏覽器。

所以我就這麼想,希望有人能給我一個解決方案和可能的解釋。

+0

可能重複://計算器。 com/questions/14220321/how-to-return-the-an-ajax-call) –

回答

1

post呼叫異步,所以在你的代碼在這裏:

$(document).ready(function() { 
    var a="szolg5"; //it will be "user defined", used in getURL.php 
    getTargetUrl(a); 
    alert(imgPath); 
}); 

...的alert發生之前post調用已完成,所以顯示的imgPath舊值。你想要做的是將一個函數傳遞給getTargetUrl,當post完成時它會調用,並將後續代碼放在那裏。

事情是這樣的:

var imgPath; // <--He is who should get the value of "data" 
function getTargetUrl(szolg, callback){ 
    $.post(
     "getURL.php", 
     { x: szolg }, 
     function(data,status){ 
      alert("in function: " + data + " status: " + status); 
      imgPath=data; 
      callback(); 
     } 
    ); 
} 
$(document).ready(function() { 
    var a="szolg5"; //it will be "user defined", used in getURL.php 
    getTargetUrl(a, function() { 
     alert(imgPath); 
    }); 
}); 

而且你完全可以做什麼post確實和數據傳回作爲參數廢除全局變量:

function getTargetUrl(szolg, callback){ 
    $.post(
     "getURL.php", 
     { x: szolg }, 
     function(data,status){ 
      alert("in function: " + data + " status: " + status); 
      callback(data); 
     } 
    ); 
} 
$(document).ready(function() { 
    var a="szolg5"; //it will be "user defined", used in getURL.php 
    getTargetUrl(a, function(path) { 
     alert(path); 
    }); 
}); 
+0

這種解決方案對我來說是最好的。感謝大家的幫助! – tormex

0

不,AJAX是異步含義,即$.post方法將立即返回。如果你想使用AJAX調用的結果,唯一安全的地方就是成功回調。不要試圖將結果分配給全局變量。

所以你應該把提醒裏面的成功回調。

0

正如其他人所解釋的,這種行爲的原因是ajax請求的異步性質。

我的解決方案將來自getTargetUrl返回阿賈克斯承諾的請求,並使用它來註冊回調方法[如何返回從AJAX響應調用?(HTTP的

function getTargetUrl(szolg){ 
    return $.post(
     "getURL.php", 
     { x: szolg }, 
     function(data,status){ 
      alert("in function: " + data + " status: " + status); 
      alert (data); 
     } 
    ); 
} 
$(document).ready(function() { 
    var a="szolg5"; //it will be "user defined", used in getURL.php 
    getTargetUrl(a).done(function(data){ 
     alert('from callback' + data); 
    }); 
}); 
相關問題