2012-06-12 36 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