0
$.getJSON(get, function (data) {

        if(data.results[0])
        { ver = data.results[0];
            $("#result").html(ver);}
            else
        $("#result").html("This source does not provide the lyrics you want. Try Sources #1 and #2");

    });
    $.post("check.php", { 'vid': 'gS9o1FAszdk', 'sursa': "1", 'vers':ver } ,function(dt){alert(ver);});

これは私のコードであり、効果はありません。データから「vers」を削除すると、スクリプトは正常に機能します。どうしたの?

4

1 に答える 1

6

AJAX は非同期であるためです。したがって、メソッドは の実行が終了するpostのを待たず、 の値を使用します。post 呼び出しを getJSON のコールバック関数に移動する必要がありますgetJSONver

$.getJSON(get, function (data) {
  var ver =""
  if(data.results[0])
        { ver = data.results[0];
            $("#result").html(ver);
        }
        else
        {  
           $("#result").html("This source does not provide the lyrics you want. Try Sources #1 and #2");
        }
       $.post("check.php", 
               { 'vid': 'gS9o1FAszdk', 'sursa': "1", 'vers':ver } ,function(dt){
                alert(ver);
       });

});
于 2012-06-12T21:13:30.873 に答える