How can I make an prototype ajax request with an array of values as a parameter?

Posted by andresbravog on Stack Overflow See other posts from Stack Overflow or by andresbravog
Published on 2012-03-28T11:05:55Z Indexed on 2012/03/28 11:28 UTC
Read the original article Hit count: 206

Filed under:
|
|
|

i'm trying to make a ajax update in prototype with some values from a multirecordselect that sends a requests like.

Parameters: {"action"=>"use_campaign", "campaigns"=> ["27929","27932"] , "advertiser_id"=>"", "controller"=>"admin/reporting", "ad_id"=>""} 

as you can see the request sends the "campaigns" elements as an array of values, i'm trying to do the same with this js code over prototype 7.

// get the campaigns 
var campaign_ids = {}; 
var campaigns = $('filter_form').getInputs("hidden","report[campaigns][]"); 
campaigns.each( function(field) {
             campaign_ids.push(field.value); 
}); 

new Ajax.Updater('ad_filter', '/admin/reporting/use_campaign', {
                method : 'get',
                asynchronous : true,
                evalScripts : true,
                parameters : {
                    'advertiser_id' : $('filter_form')['report[advertiser_id]'].value,
                    'ad_id' : $('filter_form')['report[ad_id]'].value,
                    'campaigns' : campaign_ids
                }
 });

the campaigns_ids is getting the correct info as an array like:

[ "27929", "27932" ]

but seems that prototype ajax update is sending a request like:

http://my_domain/admin/reporting/use_campaign?ad_id=&advertiser_id=&campaigns=27929&campaigns=27932

what sends parameters like:

Parameters: {"action"=>"use_campaign", "campaigns"=> "27929" , "advertiser_id"=>"", "controller"=>"admin/reporting", "ad_id"=>""}

I also tryed with

Object.toJSON(campaign_ids)

but i only get an escaped string like

Parameters: {"action"=>"use_campaign", "campaigns"=>"[\"27929\",\"27932\"]" , "advertiser_id"=>"", "controller"=>"admin/reporting", "ad_id"=>""}

There is anyway to do this as I wish?

Thanks for all.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about AJAX