Emuen
2008-Mar-06 22:59 UTC
Assigning value from a Ajax Request to a previously defined variable fails.
Hi. I have this constalation: [code] var something; new Ajax.Request("http://google.dk", { onComplete: function(e){ something = e.responseText; } }); alert(something); [/code] This does not work! Why is that? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
kangax
2008-Mar-06 23:12 UTC
Re: Assigning value from a Ajax Request to a previously defined variable fails.
Because ajax request works asynchronously (by default) and "something" is not defined by the time you alert it. Try: var something; new Ajax.Request("http://google.dk", { onComplete: function(e){ something = e.responseText; alert(something); } }); - kangax On Mar 6, 5:59 pm, Emuen <eKa...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi. > > I have this constalation: > > [code] > var something; > new Ajax.Request("http://google.dk", { > onComplete: function(e){ > something = e.responseText; > }}); > > alert(something); > [/code] > > This does not work! Why is that?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Emuen
2008-Mar-07 10:47 UTC
Re: Assigning value from a Ajax Request to a previously defined variable fails.
I dont really need to alert it, i need to use it for later evaluation, and i would like to avoid continually indenting my code into "onComplete" functions. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
T.J. Crowder
2008-Mar-07 11:58 UTC
Re: Assigning value from a Ajax Request to a previously defined variable fails.
Hi, Regardless, kangax''s point still holds: In your original code, you *initiate* a request but then go on to use the value immediately, before the request has a chance to complete. If you want to do something with the value after the request is complete, the most straight-forward way to do that is from the onComplete handler (otherwise you get into timers and polling, which is a lot more mess and no added benefit). Your code to do something with ''something'' doesn''t have to be inline in the onComplete handler, it just has to be triggered from there.> i would like to avoid continually indenting my code into > "onComplete" functionsIf you mean that literally, an indenting issue, you can rework things to remove a level or two of indentation: **** var something; new Ajax.Request("http://google.dk", { onComplete: completeHandler }); function completeHandler(e) { something = e.responseText; alert(something); } **** With more context, we might be able to suggest something even cleaner... Hope this helps, -- T.J. Crowder tj / crowder software / com On Mar 7, 10:47 am, Emuen <eKa...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I dont really need to alert it, i need to use it for later evaluation, > and i would like to avoid continually indenting my code into > "onComplete" functions.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---