flash与php 交互(as传参给php)

一种 不传参 直接读取PHP文件

btn.addEventListener(MouseEvent.CLICK,loadTxt);
function loadTxt(evt:MouseEvent):void
{
        var urlLoader:URLLoader=new URLLoader();
                urlLoader.dataFormat=URLLoaderDataFormat.VARIABLES;
        urlLoader.load(new URLRequest("http://localhost/as3/url2.php"));
        urlLoader.addEventListener(Event.COMPLETE,finish);
}
function finish(evt:Event):void
{
        var mydata:URLVariables=URLVariables((evt.currentTarget as URLLoader).data);
                mytxt.text="国家是:"+mydata.state+"\n";
                mytxt.text+="var1是:"+mydata.var1;
}
php端代码:
<?
        $state="*";
        $var1="我爱你";
        echo "state=".$state."&var1=".$var1;
?>

第二种 无参数  读取XML文件

btn.addEventListener(MouseEvent.CLICK,loadTxt);
function loadTxt(evt:MouseEvent):void
{
        var urlLoader:URLLoader=new URLLoader();
        urlLoader.load(new URLRequest("data.xml"));
        urlLoader.addEventListener(Event.COMPLETE,finish);
}
function finish(evt:Event):void
{
        var mydatas:XML=XML((evt.currentTarget as URLLoader).data);
                mytxt.text=mydatas.c1.toString()+","+mydatas.c2.toString();
}
xml文件内容:
<?xml version="1.0" encoding="utf-8"?>
<cities>
        <c1>北京</c1>
        <c2>上海</c2>
</cities>
上述代码结果:
北京,上海

第三种 无参数 PHP生成XML文件

btn.addEventListener(MouseEvent.CLICK,loadTxt);
function loadTxt(evt:MouseEvent):void
{
        var urlLoader:URLLoader=new URLLoader();
        urlLoader.load(new URLRequest("http://localhost/as3/xml.php"));
        urlLoader.addEventListener(Event.COMPLETE,finish);
}
function finish(evt:Event):void
{
        var mydatas:XML=XML((evt.currentTarget as URLLoader).data);
                mytxt.text=mydatas.c1.toString()+","+mydatas.c2.toString();
}
php端:
<?
        //你可以使用php操作xml的函数
        //这里为了简单起见只是echo出来
        echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
        echo "<cities>";
        echo "<c1>北京</c1>";
        echo "<c2>上海</c2>";
        echo "</cities>";
?>

第四种 GET 传参

System.useCodePage=true;
btn.addEventListener(MouseEvent.CLICK,loadTxt);
function loadTxt(evt:MouseEvent):void
{
        var urlLoader:URLLoader=new URLLoader();
                var request:URLRequest=new URLRequest();
                request.url="http://www.baidu.com/s";
                request.method=URLRequestMethod.GET;
                request.data="wd=flash";
        urlLoader.load(request);
        urlLoader.addEventListener(Event.COMPLETE,finish);
}
function finish(evt:Event):void
{
     mytxt.text=(evt.currentTarget as URLLoader).data;
}

第五种 POST 传参

System.useCodePage=true;
btn.addEventListener(MouseEvent.CLICK,loadTxt);
function loadTxt(evt:MouseEvent):void
{
        var urlLoader:URLLoader=new URLLoader();
                var request:URLRequest=new URLRequest();
                var vars:URLVariables=new URLVariables();
                vars.wd="flash";
                request.url="http://www.baidu.com/s";
                request.method=URLRequestMethod.GET;
                request.data=vars;
        urlLoader.load(request);
        urlLoader.addEventListener(Event.COMPLETE,finish);
}
function finish(evt:Event):void
{
     mytxt.text=(evt.currentTarget as URLLoader).data;
}
这里data是URLVariables是值对,注意区别。

上一篇:《JavaScript 闯关记》之 DOM(下)


下一篇:HDU1754 I hate it(线段树 单点修改)