2014-11-07 22 views

回答

1
var text = new Array; 
$.ajax({ 
    url: 'engine1/api.php', 
    data: "", 
    dataType: 'json', 
    success: function(rows){ 
     text = rows; 
     alert(text[0]); // will work, this gets executed after you set text 
    } 
}); 
//alert(text[0]); << don't put this here, it will get executed right after you send the request 
0

最後回答我自己的問題,對於遇到這個問題的人,這裏是我做過什麼:

因爲AJAX是異步的,alert(text[0])是越來越之前執行text=rows

您可以設置阿賈克斯這樣的程序上運行:

$.ajax({ url: 'engine1/api.php', data: "", dataType: 'json', async: false; success: function(rows){...

Apperently這是你可以/應該設置阿賈克斯async:false(因爲你JavaScript投放/ jQuery來的少數案例之一客戶端)。

+1

有沒有理由刪除異步,警報應該只是在onsuccess延續 – 2016-08-21 02:26:30