views:

38

answers:

1

I'm trying to get a return value from an ajax call but I keep getting "undefined"

function getUserName(targetName){

$.ajax({

type: "GET",
url: "http://servername/iMon/queryuser.pl?host="+targetName,
dataType: "XML",
success: function parseUserName(xml){
 var userName = $(xml).find ('firstName').text()+' '+$(xml).find('lastName').text();
 return userName;

   });
+2  A: 

You can't return a value. Ajax (Asynchronous JavaScript and XML) is Asynchronous.

The execution context is broken because parseUserName is called in response to an event being fired, it isn't called by your getUserName function.

Write the success handler to do whatever it is you want to do with the data.

David Dorward
I will just have to do a request within another function so I can use the values it gets. Thanks.
Mark Cheek