js中 如何在$.post 函数外部 访问 $.post 中的 变量

(代码如何修改,才能弹出变量issceen的值),还请高人明示
2025-03-03 08:39:38
推荐回答(2个)
回答1:

$.post默认使用的是异步执行,所以上面你那样做,很可能是alert不出来的,要修改这个问题,有两种方式,方式1(异步模式,推荐使用,这样不会页面卡死):

$.pots('/chcek/check.php', {}, function(data) {
    if (data == 1) {
        isscreen = true;
    } else {
        isscreen = false;
    }
    // 直接在这里alert
    alert(isscreen);
});


方式2(同步模式,不推荐,会卡死页面):

$.pots('/chcek/check.php', {
    async: false // false表示采用同步模式请求
}, function(data) {
    if (data == 1) {
        isscreen = true;
    } else {
        isscreen = false;
    }
});
alert(isscreen);

回答2:

var p = $.post();