19.2.从服务端脚本中读取变量

问题
我想从服务端脚本(ColdFusion, Perl, PHP, etc.)中读取变量.
解决办法
使用URLLoader.load( )方法和DataFormat.VARIABLES 读取由服务端脚本产生的URL-编码数据讨论

ActionScript读取服务端脚本数据和读取文本文件的操作是一样的,当这些数据是从服务端数据库或其他资源中产生时,脚本必须输出为URL-编码的数据才行,如果你采用perl脚本,输出为URL-编码,就不需要其他任何调整了,看下面的例子:
#!/usr/bin/perl
# In a more practical example this value would be retrieved
# from a database or other server-side resource.
$someText = "test";
# 定义Content-Type 头.
print "Content-Type: text/plain\n\n";
# Output the name-value pair.
print "someText=$someText";

ColdFusion:
+展开
-XML
<cfsetting enablecfoutputonly="yes">
<cfprocessingdirective suppresswhitespace="yes">
<cfset someText="test">
<cfoutput>someText=#someText#</cfoutput>
</cfprocessingdirective>

PHP:
+展开
-HTML
<?php
$someText = "test";
echo "someText=$someText";
?>

上面的每个脚本例子中都返回一个变量:someText,用下面的ActionScript代码读取这个变量:
+展开
-ActionScript
package {
import flash.events.*;
import flash.net.*;
import flash.util.trace;
public class Example( ) {
public function Example( ) {
// 创建URLLoader实例
var loader:URLLoader = new URLLoader( );
// 定义事件处理函数
loader.addEventListener( Event.COMPLETE, handleComplete );
// 设置读取的是URL-编码变量
loader.dataFormat = DataFormat.VARIABLES;
// 尝试读取数据
loader.load( new URLRequest( "getSomeText.cfm" ) );
}
private function handleComplete( event:Event ):void {
var loader:URLLoader = URLLoader( event.target );
trace( "someText = " + loader.data.someText );
}
}
}

如果你事先不知道变量名,可以使用for...in语句遍历data。

加支付宝好友偷能量挖...


评论(0)网络
阅读(72)喜欢(0)flash/flex/fcs/AIR