[英]jQuery callback function in callback function
I want to do something with the data I store in my var searchedUsers, the problem is that when I do the alert(searchedUsers); 我想对存储在var searchedUsers中的数据做些事情,问题是当我执行alert(searchedUsers)时; there is nothing in it. 里面什么也没有。 If I do the alert(searchedUsers); 如果我做警报(searchedUsers); under the var user code. 在var用户代码下。 There is something in it. 里面有东西。
I assume this is because the code already runs before it is fully executed. 我认为这是因为代码在完全执行之前已经运行。 Now I guess have to add another callback function in there somewhere only it doesn't seem to work I always get errors. 现在我猜想不得不在那似乎不起作用的地方添加另一个回调函数,我总是会出错。
function addFriend() {
var formData = "name=" + $("#nameForm").val() + "&familyname="
+ $("#familyname").val() + "&emailaddress="
+ $("#emailaddress").val();
var searchedUsers = [];
$.ajax({
type : "POST",
url : "ControllerJavascript?action=addFriend",
dataType : "xml",
data : formData,
success : function(xml) {
$(xml).find("user").each(
function() {
var user = $(this).find("name").text().trim() + " "
+ $(this).find("familyname").text().trim()
+ " // " + $(this).find("email").text().trim();
searchedUsers.push(user);
});
},
error : function() {
alert("An error occurred while processing XML file.");
}
});
alert(searchedUsers);
getFriends();
}
Is it possible to add another callback function so first all users are pushed to searchedUsers? 是否可以添加另一个回调函数,以便首先将所有用户推送到searchedUsers?
You already have this function: 您已经具有此功能:
success : function(xml) {
...
},
This is your callback. 这是您的回调。 Just call alert after your each method: 只需在每种方法后调用警报:
success : function(xml) {
$(xml).find("user").each(
function() {
var user = $(this).find("name").text().trim() + " "
+ $(this).find("familyname").text().trim()
+ " // " + $(this).find("email").text().trim();
searchedUsers.push(user);
});
alert(searchedUsers);
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.