简体   繁体   English

将数据从Javascript传递到Flex时出现问题

[英]Problem passing data from Javascript to Flex

I am using ExternalInterface in Flex to retrieve AMF encoded string from Javascript. 我在Flex中使用ExternalInterface从Javascript中检索AMF编码的字符串。 The problem is the AMF encoded string sometimes contains \ which causes the ExternalInterface to return null instead of the encoded string from Javascript. 问题是AMF编码的字符串有时包含\\ u0000,这会导致ExternalInterface返回null而不是Javascript中的编码字符串。

Any idea how to solve this? 不知道怎么解决这个问题?

Thanks in advance. 提前致谢。

The \\0000 is falsely interpreted as EOF when reading external data. 读取外部数据时,\\ 0000被错误地解释为EOF。 The same thing happens when it appears in XML files, as well. 当它出现在XML文件中时也会发生同样的事情。

You should be able to replace it with an unambiguous character sequence before passing the string to Flash, and back upon reception in ActionScript. 在将字符串传递给Flash之前,您应该能够使用明确的字符序列替换它,并在ActionScript中接收时返回。 In the JavaScript function, use something like 在JavaScript函数中,使用类似的东西

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

This should remove the unwanted \\0000 characters from the string before returning it to Flash. 这应该从字符串中删除不需要的\\ 0000字符,然后再将其返回给Flash。

On the Flash side, use 在Flash方面,使用

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

directly after receiving the data. 直接收到数据后。

Encoding the pyamf AMF output to base64 will do the trick. 将pyamf AMF输出编码到base64就可以了。

Here is the encoding part in python: 这是python中的编码部分:

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

Here is the decoding part in AS3: 这是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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM