I hope this question isn''t too "newbish," but I''ve been hitting my head over what should be a fairly simply problem for the past hour... Header Sent... X-JSON = {"a":1,"b":2,"c":3,"d":4,"e":5} Javascript... new Ajax.Request(''process.php?mode=json'', { method:''get'', onSuccess: function(transport){var json transport.responseText.evalJSON(true)}, /*onComplete: alert(json.a)*/ }); Firebug is reporting that json isn''t defined. Am I handling JSON wrong or what? The official documentation provided this snippet... new Ajax.Request(''/some_url'', { method:''get'', onSuccess: function(transport){ var json = transport.responseText.evalJSON(); } }); --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Inline, below. TAG On Dec 23, 2007, at 1:09 AM, Aea wrote:> I hope this question isn''t too "newbish," but I''ve been hitting my > head over what should be a fairly simply problem for the past hour... > > Header Sent... > > X-JSON = {"a":1,"b":2,"c":3,"d":4,"e":5}You''re sending this as the HTTP header? Because your code is looking for it in the response text. Which is it? If you *are* sending JSON in the headers, it should be automatically passed in as a second parameter to the function. onSuccess: function (transport, json) { alert(json); }> Javascript... > > new Ajax.Request(''process.php?mode=json'', { > method:''get'', onSuccess: function(transport){var json > transport.responseText.evalJSON(true)}, /*onComplete: alert(json.a)*/ > });I realize you''ve got onComplete commented out here, but in this code, you''ve defined the json variable as local to the onSuccess function, so it wouldn''t be available to the onComplete function ... (thus, undefined)> Firebug is reporting that json isn''t defined. Am I handling JSON wrong > or what? The official documentation provided this snippet... > > new Ajax.Request(''/some_url'', { > method:''get'', > onSuccess: function(transport){ > var json = transport.responseText.evalJSON(); > } > }); >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks for the help, now I have... function JSON_REQUEST() { new Ajax.Request(''process.php?mode=json'', { method:''get'', onSuccess: function(transport,json){myvar = json;}}); } JSON_REQUEST(); alert(myvar); myvar is not defined <- Firebug Ignoring the naming conventions, could somebody set this newbie on the right track? Literally been beating my head against the wall trying to get something which looks rather simple to be accessible to my entire program. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Are you sending X-JSON HTTP headers? TAG On Dec 23, 2007, at 3:47 PM, Aea wrote:> > Thanks for the help, now I have... > > function JSON_REQUEST() > { > new Ajax.Request(''process.php?mode=json'', { method:''get'', onSuccess: > function(transport,json){myvar = json;}}); > } > > JSON_REQUEST(); > alert(myvar); > > myvar is not defined <- Firebug > > Ignoring the naming conventions, could somebody set this newbie on the > right track? Literally been beating my head against the wall trying to > get something which looks rather simple to be accessible to my entire > program. > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Response Headers... Date Mon, 24 Dec 2007 01:51:26 GMT Server Apache/2.0.59 (Unix) PHP/5.2.5 DAV/2 X-Powered-By PHP/5.2.5 Expires Thu, 19 Nov 1981 08:52:00 GMT Cache-Control no-store, no-cache, max-age=0, must-revalidate Pragma no-cache X-JSON {"a":1,"b":2,"c":3,"d":4,"e":5} Content-Length 0 Keep-Alive timeout=15, max=78 Connection Keep-Alive Content-Type text/html As generated by Firebug. There''s no issue displaying the JSON object if I try to within that onSuccess function, but I can''t export that object. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ajax requests are by definition asynchronous. You must use alert your value from *inside* your callback, like so: function JSON_REQUEST() { new Ajax.Request(''process.php?mode=json'', { method:''get'', onSuccess: function(transport){ alert(transport.headerJSON)} }); } Also I suggest you drop using the second argument to your callback, and use the new headerJSON property instead, as the former has been deprecated. More details here: http://prototypejs.org/api/ajax/response Best, Tobie On Dec 24, 2:52 am, Aea <the...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Response Headers... > > Date Mon, 24 Dec 2007 01:51:26 GMT > Server Apache/2.0.59 (Unix) PHP/5.2.5 DAV/2 > X-Powered-By PHP/5.2.5 > Expires Thu, 19 Nov 1981 08:52:00 GMT > Cache-Control no-store, no-cache, max-age=0, must-revalidate > Pragma no-cache > X-JSON {"a":1,"b":2,"c":3,"d":4,"e":5} > Content-Length 0 > Keep-Alive timeout=15, max=78 > Connection Keep-Alive > Content-Type text/html > > As generated by Firebug. There''s no issue displaying the JSON object > if I try to within that onSuccess function, but I can''t export that > object.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks for the heads up, here''s what I have now... function JSON_REQUEST() { new Ajax.Request(''process.php?mode=json'', { method:''get'', onSuccess: function(transport){myvar = transport.headerJSON, alert(myvar);} }); } JSON_REQUEST(); alert(myvar); Still get an alert with the object, then a warning that myvar is undefined. Can somebody provide an example of how I would get that json object out of the onSuccess and into the scope of my program, either as a global variable or something that''s returned? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
As Tobie pointed out, Ajax requests are *asynchronous*, so the request fires and then the function exits while the results are being fetched. This means JSON_REQUEST will finish, and normal program flow will continue, so the alert(myvar) outside the function will get called, but, *since the request is still being processed*, myvar is still undefined. So you have to wait until the request finishes and onSuccess/onFailure get called, and then myvar will be made global. If you need to process myvar, then you have to do it from the on* callback. Best, -Nicolas On Dec 24, 2007 1:19 AM, Aea <theaea-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Thanks for the heads up, here''s what I have now... > > > function JSON_REQUEST() > { > new Ajax.Request(''process.php?mode=json'', { method:''get'', onSuccess: > function(transport){myvar = transport.headerJSON, alert(myvar);} > }); > } > > JSON_REQUEST(); > alert(myvar); > > Still get an alert with the object, then a warning that myvar is > undefined. > > Can somebody provide an example of how I would get that json object > out of the onSuccess and into the scope of my program, either as a > global variable or something that''s returned? > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Well D''oh, didn''t think that one through did I. What would be the best way for to go about this? Calling a function within onSuccess which redirects the JSON object to the code that processes it a "correct" way to do it? --~--~---------~--~----~------------~-------~--~----~ 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 Dec 24, 2007 2:15 AM, Aea <theaea-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Well D''oh, didn''t think that one through did I. > > What would be the best way for to go about this? Calling a function > within onSuccess which redirects the JSON object to the code that > processes it a "correct" way to do it?Yeah, that should work perfectly :) Best, -Nicolas> > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---