views:

4130

answers:

4

Ruby on Rails controllers will automatically convert parameters to an array if they have a specific format, like so:

http://foo.com?x[]=1&x[]=5&x[]=bar

This would get converted into the following array:

['1','5','bar']

Is there any way I can do this with an ActionScript 3 HTTPService object, by using the request parameter? For example, It would be nice to do something like the following:

var s:HTTPService = new HTTPService();
s.request['x[]'] = 1;
s.request['x[]'] = 5;
s.request['x[]'] = 'bar';

However, that will simply overwrite each value, resulting in only the last value being sent. Anyone have a better idea? I know I could just append stuff to the query string, but I'd like to do it in the POST body.

+1  A: 

I usually do something like this...


var s:HTTPService = new HTTPService();
s.url = "http://foo.com";
s.method = "post";
// add listeners...
s.addEventListenser(ResultEvent.RESULT,function(event:ResultEvent){

    mx.controls.Alert.show(event.result.toString());
});

// send the data...
s.send({
    a: 1,
    b: 5,
    c: "bar"
});


which would result in the HTTP Get / POST of:

http://foo.com?a=1&b=5&c=bar

You could also just create an associative array and pass it to the HTTPService send method, that would be something like:



var postdata:Object = {};

postdata["a"] = 1;
postdata["b"] = 5;
postdata["c"] = "bar";

// s is the HTTPService from above...
s.send(postdata);


mmattax
I need to be able to make all the post parameters have the same name. This method still uses different names for the post parameters.
Micah
I'm interested, why do you need all the parameters to use the same name?
defmeta
Ruby on Rails does some magic and converts all parameters of the same name into an array. Very handy when you need to post an array of values.
Micah
+1  A: 
PhoneTech
A: 

You mentioned that All POST parameters must have the same name. Elements that have the same name will overwrite each other in an associative array. However, I have dealt with calendar cells before, and all 31 cells belong to the Date category.

What I did was:

var params:Object = new Object; for (var i:uint=0; i<31; i++){ params["Date"+(jj.toString())] = date[i]; }

HTTPService....etc. HTTPService.send(params);

So, on the POST receiving side, it would be interpreted as Date0...Date31.

Don't know if this was what you wanted, and the post was so long ago.

--Jemmy

A: 

Come to think about it. Why don't you do an array push of all of the elements under the same index name? However, this means you are sending an array to the receiving side.

If you are POST-ing this, how will this be URL-referenced?

--Jemmy