2012-06-12 35 views
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',那麼腳本工作正常。怎麼了?我無法在jquery中發送郵寄請求

+0

不要忘記接受答案。 – SomeKittens

回答

6

因爲AJAX是異步的。因此,您的post方法將不會等待getJSON完成執行並使用ver中的值。您應該將後調用移動到getJSON的回調函數

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

}); 
+0

謝謝你,'Shyju'! –

+0

@KraYzVali:你是最受歡迎的:) – Shyju