使用 URL 加载器加载 JSON 数据

发布于 2024-11-27 21:15:41 字数 744 浏览 1 评论 0原文

我是动作脚本 3.0 的新手。帮助我了解如何使用 URLLoader 加载数据。

所以,我有一个像这样的应用程序:

var PATH:String = "http://www.somesite.com/myFirstPage.php?a=10&b=20";


var urlRequest:URLRequest = new URLRequest(PATH);
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
urlLoader.load(urlRequest);

function urlLoader_complete(evt:Event):void {
    some_result = urlLoader.data;
}

php 脚本看起来像:

<?php
//retrieve GET params

int c = a + b; //a and b come from GET request

return "{{"result_equal":"20"}}"; //result in JSON

?>

我真的不明白 .php 页面的 JSON 结果如何进入我的 URLLoader 对象。请通过简单的例子帮助我。谢谢!

I am new to action script 3.0. Help me to understand how can I load data with URLLoader.

So, I have an aplication like:

var PATH:String = "http://www.somesite.com/myFirstPage.php?a=10&b=20";


var urlRequest:URLRequest = new URLRequest(PATH);
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
urlLoader.load(urlRequest);

function urlLoader_complete(evt:Event):void {
    some_result = urlLoader.data;
}

php script looks like:

<?php
//retrieve GET params

int c = a + b; //a and b come from GET request

return "{{"result_equal":"20"}}"; //result in JSON

?>

I don't really understand how JSON result from .php page gets in my URLLoader object. Help me by simple example, please. Thanx!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

女皇必胜 2024-12-04 21:15:41

你在这里需要一些东西。首先,您需要一个 JSON 库,因为不知何故,即使使用修改后的 E4X 核心,它也没有内置到 Flash 中:

https://github.com/mikechambers/as3corelib

这应该为您提供以下代码:

import com.adobe.serialization.json.JSON;

function urlLoader_complete(e:Event):void 
{
   var loader:URLLoader = URLLoader(e.target);
   var some_result:Object = JSON.decode(loader.data);
}

现在...您的 PHP 代码一团糟。创建 JSON 的最佳方法是使用 json_encode 函数:

$retVal = array('result_equal'=>20);
echo json_encode($retVal);

You need a few things here. First off, you'll need a JSON library, because somehow it isn't built into Flash, even with their modified E4X core:

https://github.com/mikechambers/as3corelib

This should give you the following bits of code:

import com.adobe.serialization.json.JSON;

function urlLoader_complete(e:Event):void 
{
   var loader:URLLoader = URLLoader(e.target);
   var some_result:Object = JSON.decode(loader.data);
}

Now... your PHP code is a mess. The best way to create JSON is to just use the json_encode function:

$retVal = array('result_equal'=>20);
echo json_encode($retVal);
海螺姑娘 2024-12-04 21:15:41

您将需要使用此项目: https://github.com/mikechambers/as3corelib

示例用法:

<mx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.rpc.events.ResultEvent;
        import com.adobe.serialization.json.JSON;

        private function onJSONLoad(event:ResultEvent):void
        {
            var rawData:String = String(event.result);
            var arr:Array = (JSON.decode(rawData) as Array);

            var dp:ArrayCollection = new ArrayCollection(arr);

            grid.dataProvider = dp;
        }
    ]]>
</mx:Script>

<mx:HTTPService
    id="service"
    resultFormat="text"
    url="http://weblogs.macromedia.com/mesh/mashedpotato.json"
    result="onJSONLoad(event)" />

You will want to use this project: https://github.com/mikechambers/as3corelib

Example usage:

<mx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.rpc.events.ResultEvent;
        import com.adobe.serialization.json.JSON;

        private function onJSONLoad(event:ResultEvent):void
        {
            var rawData:String = String(event.result);
            var arr:Array = (JSON.decode(rawData) as Array);

            var dp:ArrayCollection = new ArrayCollection(arr);

            grid.dataProvider = dp;
        }
    ]]>
</mx:Script>

<mx:HTTPService
    id="service"
    resultFormat="text"
    url="http://weblogs.macromedia.com/mesh/mashedpotato.json"
    result="onJSONLoad(event)" />
許願樹丅啲祈禱 2024-12-04 21:15:41

PHP 代码应该如下所示:

$result = array("result_equal" => 20);
return json_encode($result);

PHP code should looks like this:

$result = array("result_equal" => 20);
return json_encode($result);
太傻旳人生 2024-12-04 21:15:41

所以,这就是我得到的:请说出什么是错的,什么是好的! Thanx1

package  
{
    import data.*;
    import flash.errors.IOError;
    import flash.events.Event;
    import flash.events.*;
    import flash.net.*;

    public class UserInfoProxy 
    {
        public static const GET_USER_INFO:DataCallConfigVO = new DataCallConfigVO( "COMMON", 'GET_USER_INFO', true, null, null);

        public function UserInfoProxy() 
        {               
            trace("Start request");
            var request:URLRequest = new URLRequest("http://www.avrora.com.localhost/myservlet.php");
            var loader:URLLoader = new URLLoader();

            loader.addEventListener(Event.COMPLETE, completeHandler);
            loader.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

            loader.load(request);
        }


        private function completeHandler(event:Event):void 
        {
            var loader:URLLoader = URLLoader(event.target);
            trace("Complete");
            trace("completeHandler: " + loader.data);
        }

        private function progressHandler(event:ProgressEvent):void 
        {
            trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
        }


        private function securityErrorHandler(event:SecurityErrorEvent):void
        {
            trace("securityErrorHandler: " + event);
        }

        private function httpStatusHandler(event:HTTPStatusEvent):void
        {
            trace("httpStatusHandler: " + event);
        }

        private function ioErrorHandler(event:IOErrorEvent):void 
        {
            trace("ioErrorHandler: " + event);
        }
    }

}

这是我的 PHP 代码,它必须返回 JSON 数据:

 <? 

//here I can process all input GET, POST params, database etc...
    $someJSONresponse = "{'username':'yozhik', 'age':'18'}";

    //here just echo result and it will be automatically downloaded??
    echo $someJSONresponse;

    ?>

So, this is what I get: Please, say whats wrong, what good! Thanx1

package  
{
    import data.*;
    import flash.errors.IOError;
    import flash.events.Event;
    import flash.events.*;
    import flash.net.*;

    public class UserInfoProxy 
    {
        public static const GET_USER_INFO:DataCallConfigVO = new DataCallConfigVO( "COMMON", 'GET_USER_INFO', true, null, null);

        public function UserInfoProxy() 
        {               
            trace("Start request");
            var request:URLRequest = new URLRequest("http://www.avrora.com.localhost/myservlet.php");
            var loader:URLLoader = new URLLoader();

            loader.addEventListener(Event.COMPLETE, completeHandler);
            loader.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

            loader.load(request);
        }


        private function completeHandler(event:Event):void 
        {
            var loader:URLLoader = URLLoader(event.target);
            trace("Complete");
            trace("completeHandler: " + loader.data);
        }

        private function progressHandler(event:ProgressEvent):void 
        {
            trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
        }


        private function securityErrorHandler(event:SecurityErrorEvent):void
        {
            trace("securityErrorHandler: " + event);
        }

        private function httpStatusHandler(event:HTTPStatusEvent):void
        {
            trace("httpStatusHandler: " + event);
        }

        private function ioErrorHandler(event:IOErrorEvent):void 
        {
            trace("ioErrorHandler: " + event);
        }
    }

}

And this is my PHP code, that must return JSON data:

 <? 

//here I can process all input GET, POST params, database etc...
    $someJSONresponse = "{'username':'yozhik', 'age':'18'}";

    //here just echo result and it will be automatically downloaded??
    echo $someJSONresponse;

    ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文