javascript - jQuery callback function in callback function -


i want data store in var searchedusers, problem when alert(searchedusers); there nothing in it. if alert(searchedusers); under var user code. there in it.

i assume because code runs before executed. guess have add callback function in there somewhere doesn't seem work 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 possible add callback function first users pushed searchedusers?

you have function:

success : function(xml) { ... }, 

this callback. call alert after 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);     }, 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -