prototypejs:保持功能直到ajax请求结果到来

时间:2010-09-21 14:07:57

标签: javascript prototypejs

我正在使用new来创建原型并使用其ajax功能,但无法将值设置为返回类型 我的代码是

var captchaRequest = new Ajax.Request('http://www.example.com/captcha/validate.php', {
       parameters: "captcha_code="+captcha_code,
   onSuccess: function(result) {

    if(result.responseText == 'true') {
     return true;
    } else {
     return false;
    }
                 alert('in ajax response');

       }
});
alert('after ajax')

我想要做的是在ajax调用完成后执行一些功能,但是我的第二个警告是“ajax之后”在“ajax响应”之前执行。 我想使用来自ajax响应的代码结果。 请帮忙。

谢谢你, 的Pankaj

1 个答案:

答案 0 :(得分:1)

您无需将其设置为异步。只需在onsuccess块中调用您的函数。

  var captchaRequest = new Ajax.Request('http://www.example.com/captcha/validate.php', {
               parameters: "captcha_code="+captcha_code,
           onSuccess: function(result) {

        if(result.responseText == 'true') {
         **callMyFunction()**
         return true;
        } else {
         return false;
        }
                     alert('in ajax response');

           }
    });
    alert('after ajax')