Problem passing data from Javascript to Flex

749 views Asked by At

I am using ExternalInterface in Flex to retrieve AMF encoded string from Javascript. The problem is the AMF encoded string sometimes contains \u0000 which causes the ExternalInterface to return null instead of the encoded string from Javascript.

Any idea how to solve this?

Thanks in advance.

2

There are 2 answers

0
doorman On BEST ANSWER

Encoding the pyamf AMF output to base64 will do the trick.

Here is the encoding part in python:

encoder = pyamf.get_encoder(pyamf.AMF3)
encoder.writeObject(myObject)
encoded = base64.b64encode(encoder.stream.getvalue())

Here is the decoding part in AS3:

var myDecoder:Base64Decoder = new Base64Decoder();
myDecoder.decode(base64EncodedString);
var byteArr:ByteArray = myDecoder.toByteArray()
byteArr.position = 0;
var input:Amf3Input = new Amf3Input();
input.load(byteArr);                
var test:MyObject = input.readObject();
2
weltraumpirat On

The \0000 is falsely interpreted as EOF when reading external data. The same thing happens when it appears in XML files, as well.

You should be able to replace it with an unambiguous character sequence before passing the string to Flash, and back upon reception in ActionScript. In the JavaScript function, use something like

return returnString.replace (/\0000/g, "{nil}");

This should remove the unwanted \0000 characters from the string before returning it to Flash.

On the Flash side, use

receiveString = receiveString.replace (/\{nil\}/g, "\u0000"); 

directly after receiving the data.