如何使用ActionScript 3.0发布的JSON数据? [英] How to post JSON data using Actionscript 3.0?

查看:323
本文介绍了如何使用ActionScript 3.0发布的JSON数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ActionScript WebServices的工作。我发现下面的code,让我用只实现GET方法的JSON的URL。但是,它不为POST方法(甚至不进入的onComplete的方法)的工作。我搜索网,并未能找到任何答案。如何使用ActionScript 3.0我POSTJSON数据?

 包
{
进口flash.display.Sprite;
进口flash.net.URLRequest;
进口flash.net.URLLoader;
进口flash.events *。
进口com.adobe.serialization.json.JSON;

公共类DataGrab扩展Sprite {

    公共职能DataGrab(){

    }

    公共职能的init(资源:字符串):无效{
            VAR装载机:的URLLoader =新的URLLoader();
            VAR要求:的URLRequest =新的URLRequest(资源);
            loader.addEventListener(引发Event.COMPLETE,的onComplete);
            loader.load(要求);
    }

    私有函数的onComplete(五:事件):无效{
            VAR装载机:的URLLoader =的URLLoader(e.target);
            VAR jsonData:对象= JSON.de code(loader.data);
            对于(VAR我:字符串中jsonData)
            {
                迹第(i +:+ jsonData [I]);
            }
    }
}
}
 

解决方案

您需要指定您的URLRequest对象使用的方法。默认为GET。这可能是第二个参数给你的的init 方法:

 公共职能的init(资源:字符串,方法:字符串=GET):无效{
    VAR装载机:的URLLoader =新的URLLoader();
    VAR要求:的URLRequest =新的URLRequest(资源);
    request.method =方法;
    loader.addEventListener(引发Event.COMPLETE,的onComplete);
    loader.load(要求);
}
 

当你调用这个函数,你可以使用静态 GET POST 的<一个特性href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLRequestMethod.html"相对=nofollow> URLRequestMethod 而不仅仅是字符串传递额外的安全性的一点点。

I have to work with webservices in Actionscript. I found the following code that allows me to use JSON URLs that implement only the GET method. However, it doesn't work for POST methods (doesn't even enter the "onComplete" method). I searched the net and was unable to find any answers. How can i "POST" JSON data using Actionscript 3.0?

package 
{
import flash.display.Sprite;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.*;
import com.adobe.serialization.json.JSON; 

public class DataGrab extends Sprite {

    public function DataGrab() {

    }

    public function init(resource:String):void {
            var loader:URLLoader = new URLLoader();
            var request:URLRequest = new URLRequest(resource);
            loader.addEventListener(Event.COMPLETE, onComplete);
            loader.load(request);
    }       

    private function onComplete(e:Event):void {
            var loader:URLLoader = URLLoader(e.target);
            var jsonData:Object = JSON.decode(loader.data);
            for (var i:String in jsonData)
            {
                trace(i + ": " + jsonData[i]);
            }
    }
}
}

解决方案

You need to specify the method used with your URLRequest object. The default is GET. This could be a second argument to your init method:

public function init(resource:String,method:String = "GET"):void {
    var loader:URLLoader = new URLLoader();
    var request:URLRequest = new URLRequest(resource);
    request.method = method;
    loader.addEventListener(Event.COMPLETE, onComplete);
    loader.load(request);
}

When you call this function you can use the static GET and POST properties of URLRequestMethod rather than just passing strings for a little bit of extra safety.

这篇关于如何使用ActionScript 3.0发布的JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆