I''m having trouble dealing with my server response using Prototype 1.5. My server is returning the results of Ajax queries in JSON (An array) and I need to eval() that in my Ajax.Request onSuccess. Unfortunately, I cannot find a way to do so. How can I get something like this: {"Success":true,"Success_Message":"Yay, PHP script returned something..."} into an array var in my Ajax.Request that would read like so: Results["Success"] Results["Success_Message"] Thanks. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I was having a similar problem and the only solution I could come up with is to use an eval (which admittingly, I don''t like to use if at all possible). This should work: var jsonStr = ''{"Success":true,"Success_Message":"Yay, PHP script returned something..."}''; var Results = eval(''('' + jsonStr + '')''); Hope that helps! Cheers! On Feb 24, 11:59 am, "RFLudwick" <rfludw...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m having trouble dealing with my server response using Prototype > 1.5. My server is returning the results of Ajax queries in JSON (An > array) and I need to eval() that in my Ajax.Request onSuccess. > Unfortunately, I cannot find a way to do so. > > How can I get something like this: > > {"Success":true,"Success_Message":"Yay, PHP script returned > something..."} > > into an array var in my Ajax.Request that would read like so: > > Results["Success"] > Results["Success_Message"] > > Thanks.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
If you''re using the Ajax.Request object, then you can use the second parameter of the callback function. However, you have to specifiy the x-json header on the server with a value of your json object. I suspect, that somewhere in the prototype code, all it''s doing is an eval() of the x-json header, but it obfuscates those details from the code. For example, in PHP, instead of simply echoing {"Success":true,"Success_message":"Yeah!"} you would say: header("x-json", ''{"Success":true,"Sucess_message":"Yeah!"}''); Then, you callback function could do something like this: function callback(response, json) { if(json.Success) alert(json.Success_Message) } Does that help? --~--~---------~--~----~------------~-------~--~----~ 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, You may also want to have a look at the upcoming JSON support. It''s available here: http://svn.rubyonrails.org/rails/spinoffs/prototype/branches/ajax/ I''d like to get as much feedback as possible... So let me know how it goes. (there''s more info on the API in trac: http://dev.rubyonrails.org/ticket/7427 and http://dev.rubyonrails.org/ticket/7295) Regards, Tobie --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---