Prototype object functions in posted ajax array

5 views
Skip to first unread message

dpre...@gmail.com

unread,
Apr 23, 2008, 12:27:37 PM4/23/08
to Ruby on Rails: Spinoffs
Anyone know why posting an array in an Ajax Request would send the
prototype functions along with my data?

function sendWarnings() {
// Grab everything with the warning class name and add
// it's value to an array.
var warnings = new Array();
$$('.warning').each(function(x, index) {
warnings[index] = x.value;
console.log(x.value, index);
});

// Post the warnings and display the response
new Ajax.Request('ajax_send.php?tst=' + $('tst').value, {
parameters: warnings,
method: 'post',
onSuccess: function(transport) {
$('multi_send_rspnce').innerHTML = transport.responseText;
}
});
}


A simple print_r from PHP gives me both the data, and a bunch of
prototype's object functions. Any idea why I'd get those and not just
the data?

Here's an easier to read version: http://www.pastie.org/185523

Justin Perkins

unread,
Apr 23, 2008, 12:47:26 PM4/23/08
to rubyonrail...@googlegroups.com
On Wed, Apr 23, 2008 at 11:27 AM, dpre...@gmail.com <dpre...@gmail.com> wrote:
>
> Anyone know why posting an array in an Ajax Request

You either want to pass a string or a Hash as the parameters value,
not an Array.

How would you like your post data to look when it comes in to the server?

-justin

Dan Previte

unread,
Apr 23, 2008, 3:17:31 PM4/23/08
to Ruby on Rails: Spinoffs
Fixed:

http://www.pastie.org/185632

Looks like $A instead of new Array() or new Object fixes it.

On Wed, Apr 23, 2008 at 11:27 AM, dpre...@gmail.com <dpre...@gmail.com> wrote:

Ken Snyder

unread,
Apr 23, 2008, 3:46:42 PM4/23/08
to rubyonrail...@googlegroups.com
Dan Previte wrote:
> Fixed:
>
> http://www.pastie.org/185632
>
> Looks like $A instead of new Array() or new Object fixes it.
Note that you can use pluck() or collect() instead of each() and push():

var warnings = $$('.warning').pluck('value');
or
var warnings = $$('.warning').collect($F);

- Ken Snyder

Justin Perkins

unread,
Apr 23, 2008, 3:48:34 PM4/23/08
to rubyonrail...@googlegroups.com
On Wed, Apr 23, 2008 at 2:17 PM, Dan Previte <dpre...@gmail.com> wrote:
> Looks like $A instead of new Array() or new Object fixes it.

Ahh, yes. I was looking at the Ajax.Base#initialize method which only
parses out Strings or Hashes. If you look a little further, at the
request method, the code converts the parameters to a query string
via: Object.toQueryString(params).

However it looks like if you do this on an Array object, you will get
a lot more than you are expecting...

Run this in Firebug: Object.toQueryString($A())

vs.: Object.toQueryString($H())

-justin

Reply all
Reply to author
Forward
0 new messages