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

注: テストのために、Google Chrome を使用しています。

誰かが私に解決策と可能な説明を教えてくれることを願っています。

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

いいえ、AJAX は非同期であるため、$.postメソッドはすぐに戻ります。AJAX 呼び出しの結果を使用する場合、安全に使用できる唯一の場所は、成功のコールバック内です。結果をグローバル変数に代入しようとしないでください。

したがって、成功のコールバックにアラートを配置する必要があります。

于 2013-03-30T09:35:41.360 に答える
0

他の人が説明したように、この動作の理由は ajax リクエストの非同期性です。

私のソリューションは、ajax promise リクエストを返し、getTargetUrlそれを使用してコールバック メソッドを登録します。

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 に答える