Send Complex Object FROM Flex to PHP

I want to be able to send complex data from Flex to PHP and be able to parse that data via PHP script. I'm able to send a basic key value pair object but anything more complex than that doesn't translate accordingly.

This works...

ht.send({label:"FOO", label2:"FAA", label3:"FII", label4:"FEE"});

It translates as expected

This doesn't work...

ht.send({obj11:{label:"FOO", label2:"FAA"}, obj2:{label3:"FII", label4:"FEE"}});

It is posted as a string [object][object].

Is it possible to send complex data to PHP? I've tried JSON.encode(object). Do I need to send XML instead?

Answers


I believe Json didn't work because you didn't set it up correctly

var myComplexObject:Object ={obj11:{label:"FOO", label2:"FAA"}, obj2:{label3:"FII", label4:"FEE"}}

var dataToSend:Object = { data: JSON.encode(myComplexObject) }

ht.send(dataToSend);



// on the php side you will have something like so

$data = json_decode(  $_POST['data'] );

echo '<pre>';
print_r( $data );

I would highly recommend using AMF instead of JSON, given it's native support in Flex.

If you are working with large datasets, I have found AMF more effective, but at the end of the day it boils down to what you're most comfortable with. AMF will be easier to work with in Flex, though.

Interesting reads:

http://web.archive.org/web/20090129160211/http://www.5etdemi.com/blog/archives/2006/12/clearing-the-fud-on-amfphps-speed-versus-json-and-xml/

http://web.archive.org/web/20090210160254/http://blogs.adobe.com/mikepotter/2006/07/php_and_flex_js.html


Need Your Help

Print JavaScript variable value inside a php tag using blade template in Laravel

jquery ajax laravel laravel-4 blade

Suppose this is a JS code and I want to print the value of id . Here id is a variable with a value receiving into the jq function.