我正在尝试编写一个泛型函数,它将填充我从服务器传入数据的任何数组。下面的代码确实正确地填充了函数中的数组,但是当我尝试传入'characters'数组时,它不会接受它。
这是函数调用:
$(document).ready(function() {
databaseConnect("loadCharacter.php", characters);
document.write(characters[0]); //This spits out 'undefined'
});
这是功能:
function databaseConnect (file, array) {
if (login == true) {
$.ajax({
type: "POST",
url: file,
data: {id : identi},
success: function(data, array) {
array = data.split('-');
i = 0;
while(array[i]) {
array[i] = JSON.parse(array[i]);
++i;
}
},
});
} else {
document.write("Dude. You're not logged in.");
}
}
答案 0 :(得分:1)
您需要了解异步性如何运作。您的代码执行如下:
你应该使用回调,因此
structure(list(V1 = structure(c(5L, 1L, 2L, 3L, 4L), .Label = c("1",
"2", "3", "4", "Id"), class = "factor"), V2 = structure(c(5L,
4L, 3L, 2L, 1L), .Label = c("2012-01-08", "2012-02-16", "2012-05-25",
"2012-07-15", "VisitDate"), class = "factor"), V3 = structure(c(5L,
3L, 1L, 4L, 2L), .Label = c("22", "33", "43", "64", "Another column"
), class = "factor")), .Names = c("V1", "V2", "V3"), class = "data.frame", row.names = c(NA,
-5L))