0

亲爱的编码员!

我的代码的目的:

获取特定文件夹中列出的文件的 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:函数(数据,状态)`。

测试.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>

获取URL.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;
}
?>

注意:为了测试我使用的是谷歌浏览器。

所以这就是我的猜测,希望有人能给我一个解决方案和可能的解释。

4

3 回答 3

2

post调用是异步的,因此在您的代码中:

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

...在调用完成之前alert发生,因此显示. 您要做的是将一个函数传递给它将在完成时调用的函数,并将后续代码放在那里。postimgPathgetTargetUrlpost

像这样的东西:

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);
    });
});
于 2013-03-30T09:34:30.453 回答
0

No, AJAX is asynchronous meaning that the $.post method will return immediately. If you want to use the results of an AJAX call, the only safe place to do so is inside the success callback. Do not attempt to assign the result to global variables.

So you should put the alert inside the success callback.

于 2013-03-30T09:35:41.360 回答
0

As explained by others the reason for this behavior is the asynchronous nature of ajax requests.

My solution will to return the ajax promise request from getTargetUrl and use it to register callback methods

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);
    });
});
于 2013-03-30T09:53:15.340 回答