从Flash AS3发送Json表单

时间:2012-03-07 05:45:48

标签: json actionscript-3 flash form-post

我有一个表格接受Asp.net中的json帖子,我需要从Flash As3调用...

我使用下面的代码来做到这一点。我看过一些他们说工作正常的帖子。 但我遇到错误

Error: Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.

这是我的代码。

        var messages:Array = new Array ();


        messages.push({"From":fromemailTxt.text,"To": ToemailTxt.text,"Body": BodyText.text,"Subject":SubjectText.text});

        var JsonObj:String = JSON.encode(messages);
        trace(JsonObj);

        var variables:URLVariables=new URLVariables(JsonObj);



        RequestURL= srvStringURL;

        var JSONLoader:URLLoader = new URLLoader();
        JSONLoader.dataFormat=URLLoaderDataFormat.TEXT;

        JSONLoader.addEventListener(IOErrorEvent.IO_ERROR, GetBookmarkURLError, false, 0, true);
        JSONLoader.addEventListener(Event.COMPLETE, parseBookmarkURLResult, false, 0, true);


        var hdr:URLRequestHeader = new URLRequestHeader("Content-type", "application/json");

        var request:URLRequest = new URLRequest(RequestURL);
        request.requestHeaders.push(hdr);
        request.data=variables;
        request.method = URLRequestMethod.POST;

        try 
        {
            JSONLoader.load(request);
        }
        catch (error:ArgumentError) 
        { 
            trace("An ArgumentError has occurred."+error.errorID.toString()); 
        } 
        catch (error:SecurityError) 
        { 
            trace("A SecurityError has occurred."); 
        }
        catch (error:Error) 
        {
            trace("Unable to load requested document.");
        }

有人对此有任何想法吗? 谢谢

2 个答案:

答案 0 :(得分:7)

错误是,因为您将错误的字符串传递给URLVariables构造函数。不要使用URLVariables。而是将数据作为字符串传递:request.data=JsonObj;

答案 1 :(得分:0)

下面是我用来使用REST Web服务并将json参数传递给它显示的服务的代码。错误#2032:流错误。 安迪想出了什么问题

var ldr:URLLoader = new URLLoader();
ldr.dataFormat = URLLoaderDataFormat.TEXT;

var strData:String = "{\"gparam\": [ {\"productid\": \"" + productId + "\"},{\"message\": \"" + mesage + "\"},{\"googleappid\": \"" + googleappid + "\"},{\"senderid\": \"" + senderid + "\"},{\"appname\": \"" + appName + "\"},{\"userid\": \"" + userId + "\"},{\"receiverid\": \"" + receiverId + "\"} ]}";

var hdr:URLRequestHeader = new URLRequestHeader("Content-type", "application/json");
var req:URLRequest = new URLRequest("http://localhost/AndroidGCM/GCMNotification.svc/SendGCM");
req.requestHeaders.push(hdr);
req.method = URLRequestMethod.POST;
req.data = strData;

trace("data: " + req.data);

ldr.addEventListener(Event.COMPLETE,onComplete);
ldr.addEventListener(IOErrorEvent.IO_ERROR , onError);
ldr.addEventListener(SecurityErrorEvent.SECURITY_ERROR ,onSecurityErr);

ldr.load(req);

function onComplete(e:Event):void
{
    trace("LOAD COMPLETE: " + ldr.data);
TextField(parMC.getChildByName("txtCheck")).appendText("\n LOAD COMPLETE: " + ldr.data);
}
function onSecurityErr(e:SecurityErrorEvent):void
{
    trace("error: " + e.text );
}
function onError(e:IOErrorEvent):void
{
    trace("error: " + e.toString());
}