Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 900
  • Last Modified:

How to httpService Post an Array of Objects in Flex

I have an application in flex which pushes objects into an array
I need to Post this this array of objects to a server script

This is a code similar to what I am using and is not working
The objects added to the array always have 2 properties (q and a) which get populated like
q:"q1",a:"answer to q1"
q:"q26",a:"answer to q26"
q:"q10",a:"answer to q10"

I would like to post this to a server script so that I can get the array like this
q1 = answer to q1
q26 = answer to q26
q10 = answer to q10

public var answers:Array = new Array();
public function setAnswer(q:String,a:String):void{
answers.push({question:q,answer:a});
}
	
public sendAnswers():void{
	myhttpService.method="POST";
	myhttpService.send(answers);
}

Open in new window

0
chuela
Asked:
chuela
  • 8
  • 4
1 Solution
 
moagriusCommented:
so you want to send the server-side script a string like you showed?

q1 = answer to q1
q26 = answer to q26
q10 = answer to q10

or do you want an array of strings?
0
 
chuelaAuthor Commented:
moaqrius,
Yes, I'd like it to post like that
qn = answer to qn
Where n can be any number
0
 
moagriusCommented:
still not sure if you want to send an array of strings, or a single string to the server side script.  if you want to post that, it'd help, otherwise if you want an array of string, just create a new array and populate it from the old one:


public sendAnswers():void{
        myhttpService.method="POST";
        var strings:Array = new Array(answers.length);
        for(var i:int=0;i<answers.length;i++){
            var answerObj:Object = answers[i];
            var answerString:String = answerObj.question + " = " + answerObj.answer;
            strings[i] = answerString;
        }
        myhttpService.send(strings);
}

of if you want to send it as a single string with line breaks, it's similar:

public sendAnswers():void{
        myhttpService.method="POST";
        var strings:Array = new Array(answers.length);
        for(var i:int=0;i<answers.length;i++){
            var answerObj:Object = answers[i];
            var answerString:String = answerObj.question + " = " + answerObj.answer;
            strings[i] = answerString;
        }
        var joinedStrings:String = strings.join("\r\n");
        myhttpService.send(joinedStrings);
}

if neither of those is what you're looking for, could you detail what the server-side script is expecting?
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
chuelaAuthor Commented:
Hi moaqrius
At the server-side, I would expect to be able to get the field-value pairs like
Qn = An

Consider an HTML form :
<input id="q1"  type="text" value="Answer to q1" />
<input id="q28" type="text" value="Answer to q28" />

So at the server level you can get each field , value like q1 = answer to q1 and q28 = Answer to q28

I have each of these field - values stored in objects added to an array
each item from the array contains a pair of field - value
Hope this makes it clear
0
 
chuelaAuthor Commented:
Also your second sample, seems to be teh solution but throws me an error :

ReferenceError: Error #1065: variable is not defined q1 = asdfasd

q2 = fasdfasd

q3 = option b

q3 = option a

q6 = option h

q4 = option d

q5 = option f

q66 = option q.
      at global/flash.utils::getDefinitionByName()
0
 
moagriusCommented:
aha - that example (the HTML) helped explain what the script expects.

try the attached snippet.

also, not sure why you'd get that error, doesn't appear to have anything to do with the string construction
public sendAnswers():void{
        myhttpService.method="POST";
        var strings:Array = new Array(answers.length);
        for(var i:int=0;i<answers.length;i++){
            var answerObj:Object = answers[i];
            var answerString:String = escape(answerObj.question) + " = " + escape(answerObj.answer);
            strings[i] = answerString;
        }
        var queryString:String = strings.join("&amp;");
        myhttpService.send(queryString);
}

Open in new window

0
 
chuelaAuthor Commented:
It still throws that error
I'm not sure why.
I'm using for the myhttpService control this instance :

<mx:HTTPService id="myhttpService"  url="http://localhost/xmltest/bin/poll.asp" />

This is the full error :
ReferenceError: Error #1065: variable not defined q1 = sdfgsdf&amp;q2 = gsdfgsdfg&amp;q3 = option%20a&amp;q3 = option%20b&amp;q6 = option%20g.
      at global/flash.utils::getDefinitionByName()
      at mx.utils::DescribeTypeCache$/describeType()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\utils\DescribeTypeCache.as:89]
      at mx.utils::ObjectUtil$/getClassInfo()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\utils\ObjectUtil.as:780]
      at mx.rpc.http::HTTPService/send()[E:\dev\3.0.x\frameworks\projects\rpc\src\mx\rpc\http\HTTPService.as:701]
      at mx.rpc.http.mxml::HTTPService/send()[E:\dev\3.0.x\frameworks\projects\rpc\src\mx\rpc\http\mxml\HTTPService.as:217]
      at Test3/sendAnswers()[C:\Documents and Settings\Usuario\Escritorio\XMLTest\src\Test3.mxml:297]
      at Test3/__btnProcess_click()[C:\Documents and Settings\Usuario\Escritorio\XMLTest\src\Test3.mxml:351]
0
 
chuelaAuthor Commented:
In fact, even If I make it like this, still throws the error :

myhttpService.send("q=something");
0
 
moagriusCommented:
agh...  sorry, i was confused - the send method does take an object, not a string of variables (i was thinking of XMLHTTP Ajax methods, where you have to pass it like that).  anyhow, the snippet below should create an object with key=>value sets for each object in the answers array:
public sendAnswers():void{
        myhttpService.method="POST";
        var parameters:Object = {};
        for(var i:int=0;i<answers.length;i++){
            var answerObj:Object = answers[i];
            parameters[answerObj.question] = answerObj.answer;
        }
        myhttpService.send(parameters);
}

Open in new window

0
 
chuelaAuthor Commented:
THNKS!
0
 
chuelaAuthor Commented:
Here's one issue :
If a question has multiple answers , line 6 of your code will only use the latest one

Think of several checkboxes in HTML
They all have the same name and each a different value which once posted to the web server
can send pairs like
Q=val1
Q=val2

In your code, for Flex, this gets replaced with a single answer!
Isn't there some PUSH option for this parameters object?
0
 
chuelaAuthor Commented:
I mean, like posting an ArrayCollection
a[0]={"q1","value1"}
a[1]={"q1","aditional value 1"}
a[2]={"q2","value2"}
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now