北城已荒凉 2010-05-16 18:01 采纳率: 0%
浏览 37

从jquery ajax调用返回

I'm trying to use the return from a jQuery ajax call in my own function, but it keeps returning undefined.

function checkUser2(asdf) {
    $.ajax({
        type: "POST",
        async: false,
        url: "check_user.php",
        data: { name: asdf },
        success: function(data){ 
            return data;
            //alert(data);
        }
    });  
}

$("#check").click(function(){
    alert(checkUser2("muma"));
});

the ajax call definitely works, because when I uncomment my alert I get the correct return, and I can see it in firebug. Am I doing something stupid?

  • 写回答

4条回答 默认 最新

  • weixin_33739541 2010-05-16 18:03
    关注

    The AJAX call is asynchronous - this means that the AJAX request is made out-of-order of usual program execution, and in your program that means that checkUser2() is not returning data to the alert.

    You cannot use return values from $.ajax() calls in this way, instead move the code that utilises the AJAX return data to the success() function - that what it's for.

    评论

报告相同问题?

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题