使用美元.Post方法将函数(数据)值返回给变量


Use $.post methods returned function(data) value to variable

亲爱的程序员们!

我的代码的目的:

获取指定文件夹中列出的文件的URL,然后在javascript中将它们赋值给Array。

我是怎么想象的:test.php中的JavaScript函数使用$.post()方法向getURL.php文件发送一个值。在此之后,getURL.php使用此值来获取特定文件夹中的特定文件url。我在$.post()方法function(data)参数中获得结果。在此之后,"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;
}
?>

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

以上就是我的猜想,希望有人能给我一个解决方案和可能的解释

post调用是异步的,所以在代码中:

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

alertpost调用完成之前出现,因此显示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);
    });
});

不,AJAX是异步的,这意味着$.post方法将立即返回。如果您想使用AJAX调用的结果,那么唯一安全的地方是在成功回调内部。不要尝试将结果赋值给全局变量。

所以你应该把警告放在 success回调中

正如其他人所解释的那样,这种行为的原因是ajax请求的异步特性。

我的解决方案将从getTargetUrl返回ajax承诺请求,并使用它来注册回调方法

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