我對我的AJAX請求的結果使用each()
,我的代碼如下:jQuery的每個Ajax的
<script type="text/javascript">
function my_function() {
str = [];
$i = 0;
$(".div_class").each(function() {
var id = $(this).attr('id');
$.ajax({
type: 'post',
url: 'my_url',
cache: false,
data: {id: id},
success: function(data) {
alert(data.result); // alert 1
str.push({$i : data.result});
}
});
$i++;
});
alert(JSON.stringify(str)); // alert 2
}
</script>
我得到結果alert(data.result)
。
問題是警報2 alert(JSON.stringify(str));
首先出現並且爲空值。
是否有人知道問題是什麼?