Title: Dealing with an array in a posted body (on the server side) Post by: HalcyonLogic on May 28, 2013, 05:58:27 PM Danijel,
Trying to understand an array concept on the server side and was hoping you could point me in the right direction. Let's say I have a javascript function that returns, amongst other things, a JSON array of values using a POST (not a GET), so values will be in the body not the header. Ex: CompanyName = ACME <--- Simple/single value followed by Employees[ "Mike", "Suzan", "Danijel", "Richard" ] <--- array Body example/source form data: CompanyName=ACME&Employees%5B%5D=Mike&Employees%5B%5D=Suzan&Employees%5B%5D=Danijel&Employees%5B%5D=Richard How do I get it back on the server side using a TrtcDataProvider.OnDataReceived event? How can I iterate though the elements of the employees array? procedure TCompanyDM.RtcDataProvider_GetEmployeesOnDataReceived( Sender: TRtcConnection ); var Srv:TRtcDataServer absolute Sender; EmployeeIndex: Integer; CompanyName: String; begin if Srv.Request.Complete then begin Srv.Request.Params.AddText( Srv.Read ); CompanyName := Srv.Request.Params[ 'CompanyName ' ] ; // This I get Ok, I get this value Srv.Request.Params[ 'Employees' ] ; // <--- However, this I don't, how do I get this array, how do I read it's properties/elements? Seems to be empty. for EmployeeIndex := 0 to ??? do begin ??? end; // Do something with it Srv.Response.Status( 200, 'Successfull' ); Srv.Response.ContentType := 'application/javascript'; end; end; Perhaps I am going about it the wrong way, please include sample code if possible. Thanks in advance, Richard Title: Re: Dealing with an array in a posted body (on the server side) Post by: Kevin Powick on May 28, 2013, 06:37:45 PM The format of your data seem a little odd. You essentially have an array in the post body named Employees[], with the values Mike, Suzan, Danijel, and Richard.
Nonetheless, you could try using the .Element and .ElementCount methods. Count := Request.Params.ElementCount['ElementName'] value := Request.Params.Element['ElementName', ndx]; Code: for ndx :=0 to Request.Params.ElementCount['Employees[]'] - 1 do Title: Re: Dealing with an array in a posted body (on the server side) Post by: HalcyonLogic on May 28, 2013, 06:51:57 PM Thanks for the tip Kevin, however, using your sample code, the elementcount is always zero.
As for the array looking a bit odd, isn't it the recommended way of posting array as described here? http://api.jquery.com/jquery.post/ See: Example: Pass arrays of data to the server (while still ignoring the return results). $.post("test.php", { 'choices[]': ["Jon", "Susan"] }); Title: Re: Dealing with an array in a posted body (on the server side) Post by: Kevin Powick on May 28, 2013, 07:03:38 PM In your original post you show the contents of the POST body. Where did you get that? Is that from Request.Params.Text? If not, please post the values from Request.Params.Text
Title: Re: Dealing with an array in a posted body (on the server side) Post by: HalcyonLogic on May 28, 2013, 07:05:54 PM Kevin would you be king enough to have a look at this example I have prepared:
http://jsfiddle.net/Yx6N7/1/ Follow the instruction at the top, you'll see how I came up with this structure. Kind Regards, Richard Title: Re: Dealing with an array in a posted body (on the server side) Post by: Kevin Powick on May 28, 2013, 07:13:08 PM I still think you should post exactly what is received by RTC. Please post results of Request.Params.Text so that you can see exactly with what you are dealing.
Title: Re: Dealing with an array in a posted body (on the server side) Post by: HalcyonLogic on May 28, 2013, 07:13:36 PM Here is my Srv.Request.Params.Text:
(The reference to "Employees" was just as an example, I am actually dealing with a "Taxes" array: AccountID=12&Amount=100&Date=2013-05-28&Taxes%5B%5D=18&Taxes%5B%5D=19&Desc=asdaad&SessionID=2602CC5A9E894DFEA92B13601627DEEA Title: Re: Dealing with an array in a posted body (on the server side) Post by: Kevin Powick on May 28, 2013, 07:19:23 PM Perhaps the issue is that the data is still URL Encoded. The %5B and %5D are brackets "[]". Have you tried the URL_Decode() function on the data prior to processing?
To expand on my original code, including the URL_Decode() function; Code: procedure TMyServer.UpdateStuff(const DS: TRtcDataServer); Title: Re: Dealing with an array in a posted body (on the server side) Post by: HalcyonLogic on May 28, 2013, 07:29:41 PM Kevin, you hit the nail on the head!
That's what's going on, I have tried adding URL_Decode and now it all works. In summary, your 2 part answer solved it: 1. Needed a URL_Decode and 2. Needed the loop mentioned above to iterate through the elements. Can't thank you enough for taking the time to help me with this. Kind Regards, Richard Title: Re: Dealing with an array in a posted body (on the server side) Post by: Kevin Powick on May 28, 2013, 07:32:39 PM Good news. Glad I could help.
Cheers, Kevin |