我有两个例子。请告诉我为什么我的变量help
在这个例子中没有按照预期工作。我检查它正在进入循环。
结果:undefined
function autopop(){
var help;
$.ajax({
type : "POST",
url : "/cgi-bin/my.pl",
data : "action=autopop",
dataType : "json",
success : function(data) {
for (var i = 0; i < data.length; i++) {
help = "test";
}
}
);
$("#id").append(help);
}
结果:test
function autopop() {
var help = "test";
$.ajax({
type : "POST",
url : "/cgi-bin/my.pl",
data : "action=autopop",
dataType : "json",
success : function(data) {
for (var i = 0; i < data.length; i++) {
help = "blub";
}
}
);
$("#id").append(help);
}
请告诉我为什么我不能从这个AJAX /循环组合中,以及如何我可以改变这个事实访问我的变种。
这是由于AJAX请求的异步性质,与其他1,092,938,932,893次相同。 –
@tymeJV是对的。关键是要记住它是异步的。如果你不知道这意味着什么,请快速阅读。 – j0hnstew
我不认为有必要降低这一点。至少他已经把他尝试过的并且正在寻求帮助。这比其他数以百万计的“给我答案”的帖子要好得多。 –