从Ajax / jQuery的呼叫使用变量的成功的功能外 [英] Use variable outside the success function from an ajax/jquery call

查看:150
本文介绍了从Ajax / jQuery的呼叫使用变量的成功的功能外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code

var test;
     $.ajax({
        type: "GET",
        url: "../views/person/controller.php?actor=person&action=checkAge",
        data: "age=" + value,
        success: function(msg){
            console.log(msg);
            test = msg; 
        },
    });
    Validate.fail(test);

现在测试VAR应该给假真像控制台不会说。 但测试VAR给了我不确定为什么?

Now the test var should give true of false like the console does say. But test var gives me undefined why?

推荐答案

大概是因为Validate.fail(测试)的异步调用之后立即发生。请记住这是异步的,这意味着它执行平行于JavaScript的网页上运行。

Probably because Validate.fail(test) occurs immediately after the asynchronous call. Remember it is ASYNCHRONOUS, meaning it executes parallel to javascript running on your page.

这篇关于从Ajax / jQuery的呼叫使用变量的成功的功能外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆