Hello, this must be something stupid but I can''t figure out how to keep JSON data recevied from AJAX call after the request is finished, the array comes out always empty. I must be doing some dump perlish assumption or doing something stupid var rows = []; document.observe( "dom:loaded", function() { new Ajax.Request( ''/ajaxrsp.html'', { method: ''get'', onSuccess: function( transport ) { rows = $A( transport.responseJSON ); rows.each( function( row, i ) { alert( row + ''/'' + i ) } ) }}) alert( rows ); } alert in the request body show the actual data but the second alert returns empty. Do I have to deep-clone it by hand or what? Please help. Thanks a lot. Robert G --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi, this is a very common question here ;o) Just add ".bind(this)" to your annonymous function call. onSuccess:> function( transport ) { > rows = $A( transport.responseJSON ); > rows.each( function( row, i ) { alert( row + ''/'' + i ) } ) > }.bind(this)})hf Bastian On Nov 17, 2007 7:13 PM, RobertG <robert.germic-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hello, > > this must be something stupid but I can''t figure out how to keep > JSON data recevied from AJAX call after the request is finished, the > array comes out always empty. I must be doing some dump perlish > assumption or doing something stupid > > var rows = []; > document.observe( "dom:loaded", function() { > new Ajax.Request( ''/ajaxrsp.html'', { method: ''get'', onSuccess: > function( transport ) { > rows = $A( transport.responseJSON ); > rows.each( function( row, i ) { alert( row + ''/'' + i ) } ) > }}) > alert( rows ); > } > > alert in the request body show the actual data but the second alert > returns empty. Do I have to deep-clone it by hand or what? Please > help. Thanks a lot. > > Robert G > > >-- -- spread the word ... see www.browsehappy.com ;o) Calvin: Weekends don''t count unless you spend them doing something completely pointless. Join the Greater IBM Connection (http://www.xing.com/premiumgroup-6291.d26b7d) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi, sure I meant "rows" instead of "this". sry. Bastian On Nov 17, 2007 8:20 PM, Bastian Feder <bastian.feder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > this is a very common question here ;o) > Just add ".bind(this)" to your annonymous function call. > > onSuccess: > > function( transport ) { > > rows = $A( transport.responseJSON ); > > rows.each( function( row, i ) { alert( row + ''/'' + i ) } ) > > }.bind(this)}) > > > hf > Bastian > > > On Nov 17, 2007 7:13 PM, RobertG <robert.germic-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hello, > > > > this must be something stupid but I can''t figure out how to keep > > JSON data recevied from AJAX call after the request is finished, the > > array comes out always empty. I must be doing some dump perlish > > assumption or doing something stupid > > > > var rows = []; > > document.observe( "dom:loaded", function() { > > new Ajax.Request( ''/ajaxrsp.html'', { method: ''get'', onSuccess: > > function( transport ) { > > rows = $A( transport.responseJSON ); > > rows.each( function( row, i ) { alert( row + ''/'' + i ) } ) > > }}) > > alert( rows ); > > } > > > > alert in the request body show the actual data but the second alert > > returns empty. Do I have to deep-clone it by hand or what? Please > > help. Thanks a lot. > > > > Robert G > > > > > > > > > > -- > -- > spread the word ... see www.browsehappy.com ;o) > > Calvin: Weekends don''t count unless you spend them doing something > completely pointless. > > Join the Greater IBM Connection (http://www.xing.com/premiumgroup-6291.d26b7d) >-- -- spread the word ... see www.browsehappy.com ;o) Calvin: Weekends don''t count unless you spend them doing something completely pointless. Join the Greater IBM Connection (http://www.xing.com/premiumgroup-6291.d26b7d) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Nov 17, 8:22 pm, "Bastian Feder" <bastian.fe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> sure I meant "rows" instead of "this". sry.Thanks for your help, it doesn''t work. I''m just half-understanding what bind do, but I now think in my example I''ve just mis-understood (or rather overlooked) that dom:loaded event is fired AFTER the rest of the script is executed as in var rows; document.observe( "dom:loaded", function() { rows = [{"first":"Robert","last":"G"}, {"first":"Honza","last":"K"}]; alert( ''1: '' + Object.inspect(rows) ); }) alert( ''2: '' + Object.inspect(rows) ); Second alert is shown first and by that time rows variable is still empty... Oh well... I should have simplified it sooner but I was blinded by this cool Ajax thing... :-( --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
developer-P5Ep+WoDybrQT0dZR+AlfA@public.gmane.org
2007-Nov-18 01:11 UTC
Re: Keep ajax respond data after request is finished
Ajax.Request is doing an asynchronous call so the alert(rows) is run before the ajax request finishes. You''ll either want to make the Ajax request synchronous or do all the logic that depends on the rows inside of the onSuccess function. Trevan RobertG wrote:> Hello, > > this must be something stupid but I can''t figure out how to keep > JSON data recevied from AJAX call after the request is finished, the > array comes out always empty. I must be doing some dump perlish > assumption or doing something stupid > > var rows = []; > document.observe( "dom:loaded", function() { > new Ajax.Request( ''/ajaxrsp.html'', { method: ''get'', onSuccess: > function( transport ) { > rows = $A( transport.responseJSON ); > rows.each( function( row, i ) { alert( row + ''/'' + i ) } ) > }}) > alert( rows ); > } > > alert in the request body show the actual data but the second alert > returns empty. Do I have to deep-clone it by hand or what? Please > help. Thanks a lot. > > Robert G > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---