如何使用ajax中的count變量?在ajax函數中,「count」顯示count,但是dessous什麼都沒有。使用變量out of ajax
var count;
$.ajax({
cache : false,
dataType: 'json',
type : "POST",
url : "count.php",
success : function(tdata){
count = tdata;
console.log(count); //this works
}
});
console.log(count); //this doesn't work
你是什麼意思它不工作?它是否說「未定義」? – Bdloul
您的ajax調用是異步的,所以您的控制檯日誌在響應之前得到打印,您可以使其同步,但這不是一個好習慣。 –
可能的重複[如何從異步調用返回響應?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) –