开发者

jquery $.ajax not returning value

开发者 https://www.devze.com 2023-01-08 17:05 出处:网络
I\'m trying to get a return value from an ajax call but I keep getting \"undefined\" function getUserName(targetName){

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号