2013-03-30 66 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); 
    }); 
}); 
相关问题