Hi all
Iam new to Prototype and I have the following question:
A PHP Script (Zend Framwork) returns the following JSON Code after
submitting a Form:
{"from_firstName":{"isEmpty":"Value is empty, but a
non-empty value is
required"},"from_lastName":{"isEmpty":"Value is
empty, but a non-empty
value is required"},"from_email":{"isEmpty":"Value
is empty, but a non-
empty value is
required"},"to_firstName":{"isEmpty":"Value is
empty,
but a non-empty value is
required"},"to_lastName":{"isEmpty":"Value is
empty, but a non-empty value is required"},"to_email":
{"isEmpty":"Value is empty, but a non-empty value is
required"}}
Now the idea is to draw a red border around all the formfields
mentioned in the JSON Code like ''from_firstName'',
''from_lastName'' etc.
So i thought I could proceed the data with evalJSON() to an object and
than use the each() method. I tried the following code:
handleJSONResponse:function(transport) {
var data = transport.responseText.evalJSON(true);
data.each(function(s) {
alert(s);
});
}
but nothing happens.. no error, nothing. I can
alert(transport.responseText) which returns me the JSON Code above.
But each() doesnt return me anything.
What do I do wrong?
Thanks for your help!
Luke The Duke
--~--~---------~--~----~------------~-------~--~----~
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 Luke,
I think you''ll find if you use a debugger like Firebug that
you''re
actually getting an error: "each is not a function". evalJSON returns
an object, but each() is a method of Enumerables like Array and Hash.
I''m pretty sure if you add this line after the eval:
data = $H(data);
...it''ll start working for you, because that creates a Hash. Details:
http://www.prototypejs.org/api/utility/dollar-h
Hope this helps,
--
T.J. Crowder
tj / crowder software / com
On Apr 23, 11:29 pm, Luke The Duke
<razi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Hi all
>
> Iam new to Prototype and I have the following question:
>
> A PHP Script (Zend Framwork) returns the following JSON Code after
> submitting a Form:
>
> {"from_firstName":{"isEmpty":"Value is empty, but
a non-empty value is
> required"},"from_lastName":{"isEmpty":"Value
is empty, but a non-empty
> value is
required"},"from_email":{"isEmpty":"Value is
empty, but a non-
> empty value is
required"},"to_firstName":{"isEmpty":"Value is
empty,
> but a non-empty value is
required"},"to_lastName":{"isEmpty":"Value is
> empty, but a non-empty value is required"},"to_email":
> {"isEmpty":"Value is empty, but a non-empty value is
required"}}
>
> Now the idea is to draw a red border around all the formfields
> mentioned in the JSON Code like ''from_firstName'',
''from_lastName'' etc.
>
> So i thought I could proceed the data with evalJSON() to an object and
> than use the each() method. I tried the following code:
>
> handleJSONResponse:function(transport) {
> var data = transport.responseText.evalJSON(true);
> data.each(function(s) {
> alert(s);
> });
>
> }
>
> but nothing happens.. no error, nothing. I can
> alert(transport.responseText) which returns me the JSON Code above.
> But each() doesnt return me anything.
>
> What do I do wrong?
>
> Thanks for your help!
>
> Luke The Duke
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
"each" is for Enumerables like Arrays, you are returning an object. http://www.prototypejs.org/api/enumerable/each So if you want to iterate over a list of Object properties you can use a "for in loop" or Object.keys(data).each(... or Object.values(data).each(... or $H(data) and perform other operations: http://www.prototypejs.org/api/hash - JDD --~--~---------~--~----~------------~-------~--~----~ 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 would suggest using this in your PHP script:
header( "Last-Modified: " . gmdate( "D, j M Y H:i:s" ) .
" GMT" );
header( "Expires: " . gmdate( "D, j M Y H:i:s", time() ) .
" GMT" );
header( "Cache-Control: no-store, no-cache, must-revalidate" ); //
HTTP/1.1
header( "Cache-Control: post-check=0, pre-check=0", FALSE );
header( "Pragma: no-cache" ); // HTTP/1.0
header(''X-JSON:(''.$json.'')'');
where $json is your JSON.
Now Prototype will automatically eval your JSON
onSuccess: function(transport.response) {
alert(response.from_firstName.isEmpty);
}
will pop up an alert box stating "Value is empty, but a non-empty
value is required".
The ouput from your PHP script is not returning an array. I think you
might be looking to return something like:
{arUsers:[{fname:''Joe'', lname:''User'',
email:''joeuser-9IKiO1iGCm/QT0dZR+AlfA@public.gmane.org''},
{fname:''Jane'', lname:''Doe'',
email:''janedoe-9IKiO1iGCm/QT0dZR+AlfA@public.gmane.org''}]}
Now you could use each to iterate over the elements you will find in
response.arUsers.
response.arUsers.each(function(s) {
// do stuff like sign them up for spam
}
HTH
Happy Prototyping
Bernie
On Apr 23, 6:29 pm, Luke The Duke
<razi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Hi all
>
> Iam new to Prototype and I have the following question:
>
> A PHP Script (Zend Framwork) returns the following JSON Code after
> submitting a Form:
>
> {"from_firstName":{"isEmpty":"Value is empty, but
a non-empty value is
> required"},"from_lastName":{"isEmpty":"Value
is empty, but a non-empty
> value is
required"},"from_email":{"isEmpty":"Value is
empty, but a non-
> empty value is
required"},"to_firstName":{"isEmpty":"Value is
empty,
> but a non-empty value is
required"},"to_lastName":{"isEmpty":"Value is
> empty, but a non-empty value is required"},"to_email":
> {"isEmpty":"Value is empty, but a non-empty value is
required"}}
>
> Now the idea is to draw a red border around all the formfields
> mentioned in the JSON Code like ''from_firstName'',
''from_lastName'' etc.
>
> So i thought I could proceed the data with evalJSON() to an object and
> than use the each() method. I tried the following code:
>
> handleJSONResponse:function(transport) {
> var data = transport.responseText.evalJSON(true);
> data.each(function(s) {
> alert(s);
> });
>
> }
>
> but nothing happens.. no error, nothing. I can
> alert(transport.responseText) which returns me the JSON Code above.
> But each() doesnt return me anything.
>
> What do I do wrong?
>
> Thanks for your help!
>
> Luke The Duke
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Oops,
After reading your post again I think you instead might want your JSON
formed like this:
{arEmptyFields:[''First Name'', ''Last Name'',
''Email'']}
B
On Apr 23, 6:29 pm, Luke The Duke
<razi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Hi all
>
> Iam new to Prototype and I have the following question:
>
> A PHP Script (Zend Framwork) returns the following JSON Code after
> submitting a Form:
>
> {"from_firstName":{"isEmpty":"Value is empty, but
a non-empty value is
> required"},"from_lastName":{"isEmpty":"Value
is empty, but a non-empty
> value is
required"},"from_email":{"isEmpty":"Value is
empty, but a non-
> empty value is
required"},"to_firstName":{"isEmpty":"Value is
empty,
> but a non-empty value is
required"},"to_lastName":{"isEmpty":"Value is
> empty, but a non-empty value is required"},"to_email":
> {"isEmpty":"Value is empty, but a non-empty value is
required"}}
>
> Now the idea is to draw a red border around all the formfields
> mentioned in the JSON Code like ''from_firstName'',
''from_lastName'' etc.
>
> So i thought I could proceed the data with evalJSON() to an object and
> than use the each() method. I tried the following code:
>
> handleJSONResponse:function(transport) {
> var data = transport.responseText.evalJSON(true);
> data.each(function(s) {
> alert(s);
> });
>
> }
>
> but nothing happens.. no error, nothing. I can
> alert(transport.responseText) which returns me the JSON Code above.
> But each() doesnt return me anything.
>
> What do I do wrong?
>
> Thanks for your help!
>
> Luke The Duke
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Hey guys, Thats the point... data = $H(data); solves my problem. Thanks and Greets from Colombia Luke On Apr 24, 3:39 am, "T.J. Crowder" <tjcrow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Luke, > > I think you''ll find if you use a debugger like Firebug that you''re > actually getting an error: "each is not a function". evalJSON returns > an object, but each() is a method of Enumerables like Array and Hash. > > I''m pretty sure if you add this line after the eval: > > data = $H(data); > > ...it''ll start working for you, because that creates a Hash. Details:http://www.prototypejs.org/api/utility/dollar-h > > Hope this helps, > -- > T.J. Crowder > tj / crowder software / com > > On Apr 23, 11:29 pm, Luke The Duke <razi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi all > > > Iam new to Prototype and I have the following question: > > > A PHP Script (Zend Framwork) returns the following JSON Code after > > submitting a Form: > > > {"from_firstName":{"isEmpty":"Value is empty, but a non-empty value is > > required"},"from_lastName":{"isEmpty":"Value is empty, but a non-empty > > value is required"},"from_email":{"isEmpty":"Value is empty, but a non- > > empty value is required"},"to_firstName":{"isEmpty":"Value is empty, > > but a non-empty value is required"},"to_lastName":{"isEmpty":"Value is > > empty, but a non-empty value is required"},"to_email": > > {"isEmpty":"Value is empty, but a non-empty value is required"}} > > > Now the idea is to draw a red border around all the formfields > > mentioned in the JSON Code like ''from_firstName'', ''from_lastName'' etc. > > > So i thought I could proceed the data with evalJSON() to an object and > > than use the each() method. I tried the following code: > > > handleJSONResponse:function(transport) { > > var data = transport.responseText.evalJSON(true); > > data.each(function(s) { > > alert(s); > > }); > > > } > > > but nothing happens.. no error, nothing. I can > > alert(transport.responseText) which returns me the JSON Code above. > > But each() doesnt return me anything. > > > What do I do wrong? > > > Thanks for your help! > > > Luke The Duke--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
JSON is actually available auto-evaled as response.responseJSON See http://prototypejs.org/api/ajax/response and http://prototypejs.org/api/ajax/options for details. Best, 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 -~----------~----~----~----~------~----~------~--~---