Forum: Rails Spinoffs (closed, excessive spam) Returning a variable with Ajax.Request

Posted by cdams (Guest)
on 2008-07-04 16:22
(Received via mailing list)
Hi,

I looked into the web, and I find that this is a common error, but I
haven't any solution..

My method Ajax.Request is in another method. And for my function(v), I
need to put a return, false or true.
I tried with my script, but it doesn't work..

Have you any idea to solve my pb?

Thanks

My script :

var valid = new Validation('form-extra', {immediate : true});
var resultat = false;

Validation.add( 'validate-captcha', 'Captcha is wrong!', function(v){
  new Ajax.Request( 'ajax_serve.php', {
    method: 'get',
    parameters: {
      validate: v
    },
    onSuccess: function( transport ) {
      if( transport.responseText.match(/true/)) {
        $('captcha').removeClassName('validation-failed');
        $('captcha').addClassName('validation-passed');
  resultat = true;
      } else {
        $('captcha').removeClassName('validation-passed');
        $('captcha').addClassName('validation-failed');
  resultat = false;
      }
    }
  });
  return resultat;
});
Posted by Diodeus (Guest)
on 2008-07-04 17:12
(Received via mailing list)
I've always found it easier to use evalScripts:true in the Ajax call
and spit back a script to call a function do do whatever I need.
Posted by cdams (Guest)
on 2008-07-04 17:41
(Received via mailing list)
What do you mean?

Can you develop a little your solution?
Posted by Diodeus (Guest)
on 2008-07-04 19:21
(Received via mailing list)
Web page:

function writeLog(someValue) {
  //write to appliction log
  now = new Date()
  url = 'http://somewhere/Logger.php?CMD='+someValue+'&now=...
  new Ajax.Request(url, { method: 'get', evalScripts:true });
}

function writeComplete(someResponse) {
       alert(someResponse)
}

 - - -

Logger.php output:

<script type="text/javascript">
writeComplete('MOO')
</script>
Posted by Frederick Polgardy (Guest)
on 2008-07-05 19:16
(Received via mailing list)
You can't do what you want because the Ajax call completes 
asynchronously -
the function won't know what value to return yet by the time it 
completes.
You either need to have your return script execute some code as one
responder suggested, or have your onSuccess/onComplete handler call some
other function when it's finished to alert your page that the Ajax call
completed.

Instead of returning the result, call another function instead:

function myCallback(returnValue) {
   /// ajax call completed with returnValue
}

new Ajax.Request(.... {
   onSuccess: function (transport) {
      .....
      myCallback(result);
   }
);

-Fred

On Fri, Jul 4, 2008 at 9:20 AM, cdams <damsvdf@gmail.com> wrote:

> Have you any idea to solve my pb?
--
Science answers questions; philosophy questions answers.
This topic is locked and can not be replied to.