Ajax Mootools变量响应

Ajax Mootools变量响应,ajax,mootools,response,Ajax,Mootools,Response,我试着将ajax响应放在变量中,稍后再使用它,但它不起作用 这是我的密码 var retourn; var requestData = new Request ({ url: 'dnscheck.php', async:false, onComplete: function(response){ //alert(response); // this work good retourn = response; } });

我试着将ajax响应放在变量中,稍后再使用它,但它不起作用

这是我的密码

var retourn;    

var requestData = new Request ({
    url: 'dnscheck.php',
    async:false,
    onComplete: function(response){
        //alert(response); // this work good
        retourn = response;
    }
});

alert(retourn); // shows me undefined
mootools 1.2.4版本

你知道怎么解决这个问题吗


在清理了历史并尝试了很多次之后,它工作得很好,谢谢

var retourn;    

var requestData = new Request ({
    url: 'dnscheck.php',
    async:false,
    onComplete: function(response){
        //alert(response); // this work good
        retourn = response;
    }
});

requestData.send();
alert(retourn); // work fine

请记住,ajax调用是异步的,因此onComplete处理程序中的代码将仅在调用返回时执行,而最终警报将在创建请求的代码之后立即执行(必须通过调用send方法开始,顺便说一句)-此时变量将始终未初始化

编辑:没有注意到您使用了aync:false(顺便说一句,这通常是个坏主意…)-但是您仍然需要发送请求

requestData.send();
编辑(2):此外,最好不要使用onComplete回调(当响应返回时调用它,甚至在检查实际返回的内容之前)——而是使用onSuccess回调,并注意响应可能失败的原因有很多,因此,您可能还需要定义onFailure回调,并在其中执行一些操作

    var retourn;    

    var requestData = new Request ({
        url: 'yoururl.php',
        async:false,
        onSuccess: function(response){
            retourn = response;
        },
        onFailure: function(xhr) {
            retourn = "failed";
        }
    });

    requestData.send();

            alert(retourn);

谢谢你的回答。但是,我应该怎么做才能等到收到回复呢?并将结果放入变量并继续执行脚本?因为当我将警报放在requestData.send()之后时,它也有相同的错误