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;
});
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.
精彩评论